Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
share|improve this question
add comment

13 Answers

up vote 7 down vote accepted

I like having each host set up with a short name in the ~/.ssh/config file on my client machine. Here's an example:

Host host1
    HostName     longhostname.mydomain.com 
    User         remoteuser
    IdentityFile ~/ssh-keys/id_rsa-my-keypair

This way I can just type "ssh host1" and immediately be logged in.

share|improve this answer
add comment

I use a combination of .ssh/config and screen:

First (as mentioned in several previous answers) I configure my .ssh/config file so that I can use short names and skip typing my username (which is not the same as my local machine's username, so it's good to have it explicitly in .ssh/config), and preferably use public keys for password-less logins.

I have an alias (or function) which lists all the hosts in my .ssh/config file, so if I forget the names of them (I have access to several dozen, and the naming schemes are wildly inconsistent and I don't use all of them as often as others) I can just type 'hosts' to see all their names listed.

Now, for screen. Since running screen on my local machine means I lose all my sessions when I take my laptop home for the night, and one of the great benefits of screen is having persistent sessions I don't run screen locally. Instead I log into a remote server and run screen on that server (let's call it "jumper" since it serves as a "jumping off point") and from there log into the other machines I'm working with.

I DON'T put them all in my .screenrc file for automatic login, because I don't want EVERY server EVERY time. I usually want some subset, so I have a screen session with one subset and another screen session with another screen subset.

share|improve this answer
add comment

The bash-completion package for linux and OS X uses the ssh known_hosts for tab-completion of host entries. That, plus a complete host file is my method.

share|improve this answer
add comment

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:

Host *-nomaster
        ControlMaster no
        ControlPath none

Host *
        ControlPath ~/.ssh/cp/%h-%p-%r

Host *-master
        ControlMaster yes

Host foobar foobar-master foobar-nomaster
        Hostname foobar.example.org

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.

share|improve this answer
add comment

I have a photographic memory so just remember their IP addresses, sad I know but of some benefit I have to say :)

share|improve this answer
3  
And here comes IPv6. –  grawity Dec 21 '09 at 13:47
    
hahaha, I've tried, I've tried - but that is a hard-limit :) –  Chopper3 Dec 21 '09 at 13:48
add comment

If you have bash as your shell you can use the following snippet to use your known_hosts file for bash_completion:

complete -W “$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ‘ ‘ | sed -e s/,.*//g | uniq | grep -v “\[“`;)” ssh

this does not work if you hash your known hosts, so you'd have to 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.

share|improve this answer
add comment

What's your favorite tool or method for quickly logging in to the hosts that you need to access via ssh?

I've used scripts, but I really like using iterm and it's bookmarks feature.

Sure, doing the clicky thing with the mouse and finding 'a' server among a list takes more time than whacking it into the command line. But iTerm does have keyboard shortcuts.

And with the bookmarks I can cluster groups of servers in logical folders (production/web or production/zones/web). I've got over a hundred servers to look after: remembering all of their names is not going to happen, let alone trying to recall which servers belong in my JDE farm, and which ones are my BOM application cluster.

Especially at 03:00 when stuff is broken.

share|improve this answer
add comment

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:

Host short
    User joni
    HostName short.foo.bar.domain.fi

(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 like ssh sh, hit tab and that's it.

This and Ctrl-R command history search make SSH'ing to my favourite hosts easy enough.

share|improve this answer
add comment
screen

Then I just leave them open.

share|improve this answer
    
This is where I started, actually. I currently have all my hosts in my .screenrc but screen has an unfortunate limit: 40 open windows at a time, and I've hit that wall. Hence the search for a new way. –  Ben Williams Dec 18 '09 at 20:28
add comment

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.

share|improve this answer
add comment

Honestly i can type ssh user@host or when i'm on windows winkey+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.

share|improve this answer
add comment

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...

share|improve this answer
4  
Ooo, I like the command completion idea. It looks like bash is pretty smart about this out of the box. I just typed "ssh <tab><tab>" and got a list of hosts that came from somewhere. I will have to look into that more. –  Ben Williams Dec 18 '09 at 18:58
    
yep, both Debian/Ubuntu and Arch have a package bash-completion with support for lots of common tools. –  grawity Dec 21 '09 at 13:47
add comment

For a GUI-based solution, look into SSHmenu.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.