Shell Person Help me keep the shell people alive.

29Apr/091

ssh Shortcuts in Bash

Good and Evil by Abbie F (C) via Flickr

(The above photo is "Good and Evil", by Abbie F (C) via Flickr).

This is the bash script I use to ssh into my fileserver (without having to type the whole IP address, etc).  It works for simply logging in with ssh, or for sending a command only.  If you want to send a series of commands, you'll need to separate each command with \; or else it (a lone semi-colon) will be interpreted as an instruction to run the command on the local machine after the first command is sent via ssh (you're 'escaping' the semi-colon, so it gets sent through ssh as a regular character, instead of its typical role of separating commands).  This method works for me, but I'd love to learn a better way if there is one.

I name the script something short and put it in my path.  I also use it in conjunction with password-less login.  I know it's not such a big deal to write out:
ssh user@ipaddress "command -arguments"
but it saves time if you're constantly ssh'ing to that machine.

Here it is in a more-legible format, that shouldn't be copied and pasted (because of the way WordPress changes quotation marks):

#!/bin/bash
SSH_ARGUMENTS=`while (($#)) ; do echo -n "$1 " ; shift ; done ; echo`
ssh administrator@192.168.0.202 "$SSH_ARGUMENTS"

And here it is in a way that's easier to copy and paste (but harder to read, in my opinion):

#!/bin/bash
SSH_ARGUMENTS=`while (($#)) ; do echo -n "$1 " ; shift ; done ; echo`
ssh administrator@192.168.0.202 "$SSH_ARGUMENTS"

Also, here's a slight variation that's useful if you have multiple machines with a sequential naming scheme, where you can specify the machine number:

#!/bin/bash
## for example:  comp 4 shutdown -r now
## note: in my setup, the username correlates with the computer's number
WORKSTATION_NUMBER=$1
shift
SSH_ARGUMENTS=`while (($#)) ; do echo -n "$1 " ; shift ; done ; echo`
ssh $WORKSTATION_NUMBER@192.168.0.10$WORKSTATION_NUMBER "$SSH_ARGUMENTS"

Edit:  I suppose anyone who knows anything about BASH would know that the entire SSH_ARGUMENTS line could be removed and the variable $SSH_ARGUMENTS replaced with $*

...every day learning a little bit more.

Comments (1) Trackbacks (0)
  1. Greetings,

    I like your scripting. I have some slight variations of those types of scripts as well. I hope you don’t mind if I post what I am using at te moment, to loop through many servers and perform a tast.

    #!/usr/bin/env bash
    ######################################################
    # Created On: Friday 9/27/2012 #
    # Reason for Script: To ssh into servers #
    # Created By: Kurt Larsen #
    ######################################################
    # Un comment for Debugging script:
    #set -xv

    ESET=”LD_LIBRARY_PATH=/usr/local/lib:/usr/local/ssl/lib:/opt/mysql/mysql/lib/mysql: PATH=/usr/sbin:/usr/bin:/usr/local/bin:/opt/mysql/mysql/bin:/mysql/admin/bin”

    #all servers
    NAME=”server1 server2 server3 server4 server5″

    #Masters
    NAME3=”server1 server2 server3 server4 server5″

    # Slave servers
    NAME2=”server1 server2 server3 server4 server5″

    GID=”$( id -g )”

    #Make sure only users that are appart of the staff group can run our script
    if [ “$GID” -ne 10 ] ; then
    echo “This script must be run as a member of the staff group” 1>&2
    exit 1
    fi

    usage(){

    echo “\nssh [a|all|s|sl|sc|scp_all|m|ma|sa|ssh_all|sshm|ssh_master]”
    echo “Will ssh or scp to all servers in arrays”
    echo ” — ./mserver.sh a ‘ls'”
    }

    if [ -z “$1” ]
    then
    usage
    exit
    fi

    case “$1” in
    “a” | “all” )
    for SERVER in ${NAME}; do
    echo “Logging into ${SERVER}”;
    #ssh ${SERVER} -t “sudo sh -c ‘$1′”
    ssh ${SERVER} -t “$ESET” ${2}
    done
    ;;
    “sa” | “ssh_all” )
    for SERVER in ${NAME}; do
    echo “Logging into ${SERVER}”;
    #ssh ${SERVER} -t “sudo sh -c ‘$1′”
    ssh ${SERVER}
    done
    ;;
    “sshm” | “ssh_master” )
    for SERVER in ${NAME3}; do
    echo “Logging into ${SERVER}”;
    #ssh ${SERVER} -t “sudo sh -c ‘$1′”
    ssh ${SERVER}
    done
    ;;
    “s” | “sl” )
    for SERVER in ${NAME2}; do
    echo “Logging into ${SERVER}”;
    #ssh ${SERVER} -t “sudo sh -c ‘$1′”
    ssh ${SERVER} -t “$ESET” ${2}
    done
    ;;
    “m” | “ma” )
    for SERVER in ${NAME1}; do
    echo “Logging into ${SERVER}”;
    #ssh ${SERVER} -t “sudo sh -c ‘$1′”
    ssh ${SERVER} -t “$ESET” ${2}
    done

    ;;
    “sc” | “scp_all” )
    for SERVER in ${NAME}; do
    echo “scping to ${SERVER}”;
    #ssh ${SERVER} -t “sudo sh -c ‘$1′”
    scp -r -C ${2} ${SERVER}:${3}
    done
    ;; *)


Leave a comment

No trackbacks yet.