Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

SSH to multiple boxes via tab completion UNIX
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.
    •    
  • Currently 2.25 / 5
  You rated: 2 / 5 (4 votes cast)
 
[9,213 views]  

SSH to multiple boxes via tab completion | 22 comments | Create New Account
Click here to return to the 'SSH to multiple boxes via tab completion' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
SSH to multiple boxes via tab completion
Authored by: EddEdmondson on Oct 14, '04 11:37:56AM
I don't see why you don't use aliases really? I have a bunch of aliases in my .bashrc along the lines of

alias box1='ssh user1@box1.com'
alias box2='ssh user1@box2.com'
alias box3='ssh user2@box3.com'
and so on. I can have a username/boxname combination for any of these, a myriad of other options for tunnelling and the like, and they all tab complete just fine thanks, plus I don't have a folder full of scripts - they're all tidied away in the one hidden file.

PS - ssh to root?? tutut ;-)

[ Reply to This | # ]

SSH to multiple boxes via tab completion
Authored by: sidekickr on Oct 14, '04 11:42:09AM

To have the commands work in the terminal without executing bash, you can create a copy of your .bashrc called .profile or make .profile linked to the .bashrc file.



[ Reply to This | # ]
SSH to multiple boxes via tab completion
Authored by: EddEdmondson on Oct 14, '04 11:48:36AM
I suspect it works ok for me as I have the following in a .bash_profile file

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi
I honestly can't remember where I picked that one up from though.

[ Reply to This | # ]
SSH to multiple boxes via tab completion
Authored by: neror on Oct 14, '04 11:52:26AM
iTerm makes it even easier. You can set up profile bookmarks and open up a new ssh session with a simple shorcut key combo. You can open up the sessions in tabs or in separate windows. Not to mention, you can fully cusomize the profiles to use different terminal emulations and and key mappings, etc. I use iTerm just for this reason (and the tabs, of course).

If you're set on using the OSX Terminal, though. You can use a recent feature in Quicksilver called triggers that allows you to bind keystrokes to any QS command or AppleScript. You could write an AppleScript that opens a terminal window and a specific ssh session and bind it to a keystroke.

I still prefer iTerm. I haven't used the OSX Terminal in over a year.



[ Reply to This | # ]
SSH to multiple boxes via tab completion
Authored by: EddEdmondson on Oct 14, '04 12:03:04PM
JellyfiSSH is also handy if you prefer a bookmark manager in your dock. I used to use it but now just go for the alias option.

[ Reply to This | # ]
You can use the tab completion that's built into bash
Authored by: TheSpoonman on Oct 14, '04 11:49:39AM

Since bash 2.04 you've been able to do this within bash "naturally". You can find out more information, as well as get a collection of commonly used completion routines at:

http://freshmeat.net/projects/bashcompletion/

As an example of what you want to do, you can program bash to complete an ssh command by tabbing through your ssh_known_hosts file.

---
---
Answering the age-old question: which is more painful, going to work
or gouging your eye out with a spoon?
www.workorspoon.com



[ Reply to This | # ]
You can use the tab completion that's built into bash
Authored by: TrentC on Oct 15, '04 04:39:31PM

Thank you! I've gotten so spoiled with autocompletion on my Linux servers and I couldn't figure out where the file was hidden in Panther. Turns out it's not there (silly Apple!)

I'm much happier now...



[ Reply to This | # ]
SSH to multiple boxes via tab completion
Authored by: neror on Oct 14, '04 11:53:47AM
iTerm makes it even easier. You can set up profile bookmarks and open up a new ssh session with a simple shorcut key combo. You can open up the sessions in tabs or in separate windows. Not to mention, you can fully cusomize the profiles to use different terminal emulations and and key mappings, etc. I use iTerm just for this reason (and the tabs, of course).

If you're set on using the OSX Terminal, though. You can use a recent feature in Quicksilver called triggers that allows you to bind keystrokes to any QS command or AppleScript. You could write an AppleScript that opens a terminal window and a specific ssh session and bind it to a keystroke.

I still prefer iTerm. I haven't used the OSX Terminal in over a year.



[ Reply to This | # ]
SSH to multiple boxes via tab completion
Authored by: vdanen on Oct 14, '04 12:24:38PM
This kind of thing is what ~/.ssh/config is for. Very easy to use. For instance, to ssh into host1.somewhere.com as root you would add:

Host host1
  Hostname host1.somewhere.com
  User root
Then all you have to do is "ssh host1". Nice thing about this is you can set other options as well. For instance, I like to log into one of my webservers through a firewall that does port forwarding on a non-standard port to my webserver. I also need to ssh into the firewall box as well which is why I use a different port number, but I have it setup like this:

Host web
  Hostname 10.0.5.4
  Port 2022
  HostKeyAlias web
  CheckHostIP no
  ForwardX11 yes
  ForwardAgent no
  User vdanen
Now all I have to do is
ssh web
and I get logged in to port 2022 on the firewall host (10.0.5.4) which is forwarded to port 22 on the firewall. The HostKeyAlias and CheckHostIP clauses are necessary because otherwise ssh complains because I have the hostkey for 10.0.5.4 in ~/.ssh/known_hosts which is different from the host key for webserver ("web"). Use
man ssh_config
for more details, but I find using ~/.ssh/config for this kind of thing far more flexible than using bash aliases.

[ Reply to This | # ]
SSH to multiple boxes via tab completion
Authored by: abiss on Oct 14, '04 01:26:47PM

If your servers use static IPs, and that is recommanded, you can just add an entry in /etc/hosts for each of your servers.

You will then be able to use tab-completion after the @

exemple :
/etc/hosts contains :
1.2.3.4 myserver1
1.2.3.5 myserver2

type in terminal :
ssh root@ and hit tab and it will show you the list of your servers



[ Reply to This | # ]
ssh'ing in as root is a bad idea
Authored by: bentucker on Oct 14, '04 01:49:31PM

Okay, I know this isn't what this post is about, but logging into boxes as root is a just plain bad idea. The first thing you should do when setting up a machine is to disable ssh root login (it's in the .ssh/config file). Login as yourself and then use sudo as needed or in extreem cases /bin/su -. It's a MUCH smarter and safer approach.



[ Reply to This | # ]
ssh'ing in as root is a bad idea
Authored by: extra88 on Oct 14, '04 02:35:24PM

I agree but the given file name/path is incorrect. To disable logging in as root for the SSH server, edit the file /etc/sshd_config

Find this line:
#PermitRootLogin yes

and change it to this:
PermitRootLogin no

If there is no PermitRootLogin line in the config file (OS X includes it by default but I'm not sure all do), just add the line anywhere in the file.

Once you've finished editing the config file, stop and start the ssh server to load the updated config.



[ Reply to This | # ]
ssh'ing in as root is a bad idea
Authored by: epsilon on Oct 14, '04 04:22:36PM

ironic - just reading this from work, thought it would be a good idea... had to log into root via SSH to change it ;) lol



[ Reply to This | # ]
ssh'ing in as root is a bad idea
Authored by: bentucker on Oct 14, '04 08:14:55PM

Thanks for the config file correction! Sorry about that.



[ Reply to This | # ]
sudo su - is another bad idea
Authored by: datasmid on Oct 14, '04 05:03:39PM

Yeah!!! and fix the sudoers file when you can!

MacOSX sudo su - gives root to any admin, sudo was meant to keep users from suing to root and to keep them from editing sudoers. sudo was meant to delegate SOME commands to SOME users from SOME hosts.

PROPOSED FIX
1 Enable the root user in NetInfo
2 Populate the wheel group in NetInfo with the user allowed to become root
3 sudo visudo enter the hashmark like:
#%admin ALL=(ALL) ALL
4 chmod o-rx /usr/bin/su



[ Reply to This | # ]
sudo su - is another bad idea
Authored by: bentucker on Oct 14, '04 08:21:49PM

Yes. Also it's a good habit to explicitely run /bin/su -, rather than just su -. Reduces chance of someone throwing another su into the search path and grabbing your password.



[ Reply to This | # ]
sudo su - is another bad idea
Authored by: EddEdmondson on Oct 15, '04 05:08:19AM
No, that's all back to front, surely?

I'd assume that an 'admin' user is allowed to do root operations, and sudo just helps prevent them doing a lot of them by accident.

If you don't want a user to be root then don't make them an admin. You can tweak the sudoers file to give them any additional permissions beyond normal.

Admittedly Apple's use of 'sudo' here is perhaps a bit non-standard but then the idea of 'admin' users is just about as non-standard.

[ Reply to This | # ]

Or... just say "zsh"
Authored by: pmccann on Oct 14, '04 08:18:01PM

In yet another "or just..." reply, we have: just switch to a nicer shell. If you use zsh as your shell the host completion stuff is implemented very nicely by default. You just add a list of hosts to your .zshrc file (in the form

hosts=(host1.example.com host2.example.com another.machine.com)

and tab completion of the host applies not just to the ssh command, but to scp, sftp and --as something of a killer feature, especially for users happy with setting up ssh keys-- you also get tab completion of filenames on the remote host when using scp. Beautiful!



[ Reply to This | # ]
Or... just say "zsh"
Authored by: Titanium Man on Oct 14, '04 09:18:11PM
Bits of my .zshrc which pull hosts out of ~/.ssh/known_hosts (I stole this from somewhere and modified it) and enable the tab completion:
autoload -U compinit
compinit -C -d ~/.zcompdump_$ZSH_VERSION
typeset -aU hostnames
hostnames=(
host1.example.com
host2.example.com
host1.otherexample.com
)
[[ -f $HOME/.ssh/known_hosts ]] && hostnames+=(
${${${${(f)"$(
     

[ Reply to This | # ]
Or... just say "zsh"
Authored by: Titanium Man on Oct 14, '04 09:20:46PM

Yikes, that should look like this:

autoload -U compinit
compinit -C -d ~/.zcompdump_$ZSH_VERSION
typeset -aU hostnames
hostnames=(
host1.example.com
host2.example.com
host1.otherexample.com
)
[[ -f $HOME/.ssh/known_hosts ]] && hostnames+=(
${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[0-9]*}%%\ *}%%,*}
)
zstyle ':completion:*:*:ssh:*:hosts' hosts $hostnames
zstyle ':completion:*:*:ftp:*:hosts' hosts $hostnames
zstyle ':completion:*:hosts' hosts $hostnames



[ Reply to This | # ]
SSH to multiple boxes via tab completion
Authored by: optimusprime on Oct 15, '04 06:28:40PM

Terminal can save all window sessions and their commands into a single clickable .term file. Do File -> New Command for each host and type in the ssh command. Once you have all the windows open, File -> Save As and select All Windows. Now, running the term file will open all your sessions.



[ Reply to This | # ]
Neater (and native) Solution
Authored by: cynikal on Oct 20, '04 11:53:29AM

I'm not sure if many people know about this or use it, but Apple's Terminal has a built-in session management system as well.. simply press cmd+shift+k and it will bring up a list of hosts detected on your network (via rendezvous).. though i usually just edit my com.apple.Terminal.plist and put all the hosts i ever need to connect to in there..

and that's the simplest way to manage multiple hosts.. as far as loggin in as root, if the other guy didn't say it, i would have: it's a bad idea. if you must, edit your config file in the ~/.ssh dir and you can set the defautl login name to be root by just putting 'User root' on a line by itself..



[ Reply to This | # ]