Auto ssh over a set of machines

I just found out that every pc in the lab have sshd running and waiting at port 22.
Now I immediately thought some nice and nasty things like:

  • running the same process everywhere
  • do some funny jokes to the users connected
  • etc etc

    Or simply get a shell with all the laboratory software you need automatically.

    If you also have on those machines a listening xorg then you could also use X11 programs over the ssh, which works like a charm. (ssh -Yc “hostname” “command” for example)

    The only two “problems” are:

  • the need to insert the password every time
  • the need to accept the fingerprint for each new pc
  • Let’s see how to solve them.

    First we generate a new key:

    ssh-keygen -t dsa -f $HOME/.ssh/unilab

    Then we upload the public key to one of the pc’s, as long as the home directory is mounted over the network (of course) it doesn’t matter which one.

     scp $HOME/.ssh/unilab.pub utente@host:.ssh/ 

    Last thing you need to add to the authorized keys your public key (on whatever lab pc you’re logged in).

     cat $HOME/.ssh/unilab.pub >> $HOME/.ssh/authorized_keys 

    Now we can set a really nice configuration in our $HOME/.ssh/config.

    Here it is:

    Host a10?pc*
      User <youruser>
      IdentityFile <your private key>
      Port 22
      StrictHostKeyChecking no
    

    And that’s it, if for example you now do

     ssh a105pc08 

    ssh will find the matching on the regular expression and use those settings, letting you login automatically.

    But we’re not over, how do we know where sshd is up and running?


    We’ll we can do a scan of a list of hosts given in input until we find an ssh up and running

    #!/usr/bin/env bash
    # usage cluster.sh <command> 
    # gives you the first possible shell launching the optional command
    
    NC="/opt/local/bin/nc"
    FPING="/opt/local/sbin/fping"
    
    function get_first_shell {
    	for host in $(lab5)
    	do
        echo "analyzing $host"
        if $FPING -t50 $host > /dev/null
          then 
      		if $NC -z -w 1 $host 22 > /dev/null
      		then
      	    open_shell $host $1 && break
      		fi
    		fi
    	done
    }
    
    function open_shell {
      # -t useful lo launch remote interpreters
    	ssh -t $1 $2; return
    }
    
    function lab5 {
    	for ((i=1; $i < 40; i=$i+1))
    	do
    		echo "a105pc$(printf '%02d' $i)"
    	done
    }
    
    get_first_shell $1
    

    This is just an example, it’s enough to substitute the lab5 function with a static list or a generator of your hostnames/ip address you want to scan.
    The function get_first_shell first checks that the host is up with fping and then checks that the port 22 is open with netcat , and that’s it!
    This is an example of execution:

    andreaMb:~ andrea$ get_first_shell 
    analyzing a105pc01
    analyzing a105pc02
    analyzing a105pc03
    analyzing a105pc04
    analyzing a105pc05
    analyzing a105pc06
    analyzing a105pc07
    analyzing a105pc08
    Linux austudNONETWORK 2.6.24-23-generic #1 SMP Thu Nov 27 18:44:42 UTC 2008 i686
    
    The programs included with the Ubuntu system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    
    Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
    applicable law.
    
    To access official Ubuntu documentation, please visit:
    http://help.ubuntu.com/
    Last login: Thu Mar 19 12:22:31 2009 from 172.31.121.180
    andrea.crotti@a105pc08:~$
    

    Now we have the automatic access, GREAT!

    Advertisement

    One Response to Auto ssh over a set of machines

    1. Silvano says:

      parallel ssh might me interesting for you to run the same command on differents machines running sshd.
      http://www.theether.org/pssh/
      ciao
      silvano

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Connecting to %s

    Follow

    Get every new post delivered to your Inbox.