ssh autocomplete

complete -W "$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | uniq | grep -v "\["`;)" ssh
Add to your bash profile to minimize carpal tunnel syndrome. Doesn't work with [email protected] but appending "-l user" works fine if needed. Works for ping as well.. complete -W "$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | uniq | grep -v "\["`;)" ping
Sample Output
$ssh myserver.<tab>

By: viner
2009-07-23 15:59:55

8 Alternatives + Submit Alt

What Others Think

Doesn't work very well on a modern SSH system where the known_hosts file is hashed. It's a good idea though.
ajt · 572 weeks ago
@ajt to turn off hostname hashing, you may change either /etc/ssh/ssh_config or ~/.ssh/config and change HashKnownHosts from yes to no. . The approach shown here is cute, but there is a package in Debian/Ubuntu named 'bash-completion' which extends bash's command completion to help you tab-complete program *arguments*, like ssh hostnames or image/gimp file names when running gimp.
bwoodacre · 572 weeks ago
@bwoodacre however the know_hosts file is hashed for a reason - to prevent "bad" scripts from hoping from server to server...
ajt · 572 weeks ago
@ajt I read your comment as you saying there was no way to turn off that hashing and so mentioned how to do it. Obviously there is a reason that hashing is turned on by default for added security, but it is still an option that may be turned off by the user if they so choose.
bwoodacre · 571 weeks and 3 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: