Multi (file)source SSH host tab-completion

complete -W "$( { awk '/^Host / { print $2 }' ~/.ssh/config | egrep -v '\*|,' echo $( grep '^ssh ' .bash_history | sort -u | sed 's/^ssh //' ) while IFS=' ,' read host t; do echo $host; done < ~/.ssh/known_hosts ;} )" ssh
This is a multi-sourced hostname tab completion command you can put in your .bashrc (or your .bash_profile on a MAC). This will not only your .ssh/config, it will parse ALL of the following files for hostnames: .ssh/config .ssh/known_hosts .bash_history (I realize this could be prettied up by defining and calling the function, instead of implicitly referencing it, but this was more breif)
Sample Output
[user@host ~]$ ssh 10.160.<TAB>
10.160.14.131   10.160.14.38    10.160.15.102   10.160.15.73    10.160.232.166  10.160.232.200  10.160.232.206  10.160.232.215  10.160.232.37   10.160.233.70   10.160.52.24    
10.160.14.154   10.160.14.7     10.160.15.171   10.160.232.142  10.160.232.167  10.160.232.201  10.160.232.208  10.160.232.216  10.160.232.43   10.160.233.72   10.160.52.25    
10.160.14.157   10.160.14.8     10.160.15.213   10.160.232.143  10.160.232.173  10.160.232.202  10.160.232.211  10.160.232.229  10.160.232.46   10.160.233.83   
10.160.14.165   10.160.14.9     10.160.15.24    10.160.232.157  10.160.232.174  10.160.232.203  10.160.232.212  10.160.232.23   10.160.232.47   10.160.233.90   
10.160.14.169   10.160.15.100   10.160.15.26    10.160.232.160  10.160.232.175  10.160.232.204  10.160.232.213  10.160.232.28   10.160.232.85   10.160.233.91   
10.160.14.25    10.160.15.101   10.160.15.65    10.160.232.161  10.160.232.199  10.160.232.205  10.160.232.214  10.160.232.36   10.160.233.68   10.160.233.93   

0
2012-02-28 18:08:53

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?

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

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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: