Thursday, September 20, 2012

SSH Sessions in multiple tabs from one command

Seriously, aren't you tired of typing or clicking hundreds of times to get to hundreds of remote linux machines? Well I got tired today and I have to confess I barely interact with less than a dozen of them and only when there are serious issues. I cannot imagine what the life of those issuing commands the whole day to linux servers can be without having a way to open up connections to a whole farm of servers from just one command.

I created run-in-tabs.sh which only works for the gnome-terminal. You can do similar stuff with iTerm or plain Terminal plus Applescript in OSX even though that is out of the scope of this post as I am trying to push the team to work with Desktops that are closer to the servers where the applications are hosted.

Suppose you have server1 accessible, server2 and server 3 accessible from server1 and server 4 only accessible from 3 and 2. It is not hard to find this kind of situations especially in environments where security must be put in place with scarce resources. What do you do?
  1. Download the script.
  2. Create wrappers for your SSH connections especially if they are multi-hop
  3. Include the commands in a file like billing-environment.txt
    #billing-environment.txt
    
    #A simple direct ssh
    ssh -t user@server1
    #Wrapped (for simplicity) ssh commands
    /home/nestor/ssh-billing-server2.sh
    /home/nestor/ssh-billing-server3.sh
    /home/nestor/ssh-billing-server4.sh
    #A complex direct ssh equivalent to /home/nestor/ssh-billing-server4.sh
    #ssh -t user@server1 \"ssh -t server2 \"ssh -t server4\"\"
    
    Here is for example /home/nestor/ssh-billing-server4.sh:
    ssh -t user@server1 "ssh -t server2 \"ssh -t server4\""
    
  4. Run just one command and get the 4 tabs with an ssh session to a different server each:
    $ ./run-in-tabs.sh billing-environment.txt
    

No comments:

Followers