7

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

13 Answers 13

13

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.

2

For a GUI-based solution, look into SSHmenu.

2

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

2
  • 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. Dec 18, 2009 at 18:58
  • yep, both Debian/Ubuntu and Arch have a package bash-completion with support for lots of common tools. Dec 21, 2009 at 13:47
2

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.

2

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

2
  • 6
    And here comes IPv6. Dec 21, 2009 at 13:47
  • hahaha, I've tried, I've tried - but that is a hard-limit :)
    – Chopper3
    Dec 21, 2009 at 13:48
1

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.

1

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.

1

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.

1

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

0
screen

Then I just leave them open.

1
  • 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. Dec 18, 2009 at 20:28
0

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.

0

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.

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .