SSH to multiple boxes via tab completion

Oct 14, '04 10:28:00AM

Contributed by: lethalfrog

I have to logon as root to over 20 unix servers, it can get tedious to keep typing ssh -l root; yes, an alias would at least remove the need for -l, but this way I can be really lazy and use tab complete. Create a folder within your home folder (servers, for example), and then add it to the path so that any command added there can be run and tab completed. Create/modify .bashrc and add the following line:

PATH=$PATH:/Users/short_username/servers ; export PATH
Now create the following script:
!#/usr/bin/bash
# Strip the full path from the name
export BOX=`echo $0 | rev | sed 's/\/.*//' | rev`
# Now ssh to the name used as arg 0 e.g. the machine name
ssh -l root ${BOX}
Put this script (make it executable with chmod +x script_name) into the new folder, and create symbolic links with the hostname of each box you need to connect to. For example, if the script was saved as sshwrap, then do the following:
ln -s sshwrap _servername_    ## change _servername_ to hostname
Now when you want to ssh to a box, just type the hostname anywhere and you can even tab complete for laziness. This seems to work perfectly in Xterm but not in the terminal; I have to run bash first, and then it works.

Comments (22)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2004101116572691