How can I quickly select and log in to the multiple hosts that I need to access via ssh? I'm building a shell script like the one below that uses dialog
to show a menu of hosts from the command line but I'm curious whether there's a better way.
#!/bin/dash
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15
while [ 1 ]
do
dialog --menu "Select Host" 0 0 0 \
"hostname1" "Host description 1" \
"hostname2" "Host description 2" \
"hostname3" "Host description 3" 2> $tempfile
HOSTNAME=`cat $tempfile`
if [ "x" = "x$HOSTNAME" ]; then
break
fi
ssh $HOSTNAME
done
I like having each host set up with a short name in the ~/.ssh/config file on my client machine. Here's an example:
This way I can just type "ssh host1" and immediately be logged in.
For a GUI-based solution, look into SSHmenu.
For the frequent logins I usually depend on short hostnames. Our company DNS contains most of them, the rest is in /etc/hosts. Combined with an ssh agent this is convenient enough for me. Oh yes, and: muscle memory. When I think of a hostname it magically appears on the screen if my fingers are close enough to the keyboard.
For the less frequent commands I usually depend on either the command history (if they're not that infrequent) or the Wiki where they are documented (copy/paste). This approach has the advantage that the less frequent tasks are more or less guaranteed to be documented completely.
I've thought about playing with automating command completion, so that ssh will automatically expand frequent hostnames on "tab", but up until today I've not missed this too much... One day I will...
Honestly i can type
ssh user@host
or when i'm on windowswinkey+r then putty <server>
faster than any menuing system. That and I avoid using the mouse as much as possible because those ticks of the clock add up at the end of the day.I have a photographic memory so just remember their IP addresses, sad I know but of some benefit I have to say :)
I prefer either little shell scripts or shell aliases. In the finest UNIX tradition, I name them as short as possible to minimize typing.
For example, I have an alias "ns1" which is the 1-line SSH command needed to login into the (obviously) first name server I maintain.
Usually the command name is a short mnemonic for the machine name, and the un-prefixed version of that name defaults to ssh. So "ns1" will ssh me in, and (were it a Windows box) the alias "rns1" might fire off a remote desktop client command to that machine.
If I had so many that I couldn't keep track of them all, I'd use the shell script method, and keep a description line on a comment in each one, with a common tag for all scripts. Then I'd write a small script that would print out the name of each file in my custom script directory followed by whatever I grepped from that comment line. Running that one script would then document to the screen each command and what it did.
I just learned a few days ago that you can use
~/.ssh/config
to make this quite painless.Entries in that file look like this:
(
User
can omitted in which case you current username is used.)Then you can use the short hostname:
ssh short
. Or with bash-completion enabled, you'd just type something likessh sh
, hit tab and that's it.This and Ctrl-R command history search make SSH'ing to my favourite hosts easy enough.
If you have bash as your shell you can use the following snippet to use your known_hosts file for bash_completion:
this does not work if you hash your known hosts (what Ubuntu does by default), you can deactivate it by setting
HashKnownHosts no
in/etc/ssh/ssh_config
, or you parse something else like your /etc/hosts file.osx port for bash_completion already includes support for ssh tab completion
scp tab completion is pretty sweet too, but requires the use of ssh-agent or an ssh_config to reference a password-less identity file with a corresponding public key on the remote host.
If you want to speed up the connection, then using the ControlPath/ControlMaster functionality of the OpenSSH client helps and takes advantage of most connections being lightly used, so that it's okay to multiplex multiple logins over one TCP connection.
~/.ssh/config
is parsed so that for any configuration option, the first match found is used, so you can do something like this:and you'll need to
mkdir ~/.ssh/cp
too, of course.I then just use the tab-completion of
zsh
which comes with tab-completion for many commands, including ssh. For ssh, it will prefer hosts from the known_hosts files (both user and system) but if the host prefix doesn't match those, will use ~/.ssh/config host rules.Then I just leave them open.