Archive for March, 2008

bash autocomplete for ssh

hi,

long time i havent posted anything. but here is something that i cooked up over last weekend. the use case is that i keep sshing to a set of machines from my box. And its difficult to remember the exact host names and stuff. so i wanted something like autocomplete we use for ls or vim commands. Without much ado here is the code for making my thing to work.

add this text to your .bashrc or .bash_profile

_compssh ()
{
cur=${COMP_WORDS[COMP_CWORD]};
COMPREPLY=($(compgen -W '$(/bin/cat ~/hosts_list) --all --schema' -- $cur))
}
complete -F _compssh ssh

and the contents of ~/hosts_list are something like this

database1.silver.sympa
webserv1.silver.sympa
data1.gold.sympa

dont mind the names they are servers internal to my network. but you can edit ~/hosts_list file for any number of hosts
Then just sit back and do ssh +[tab] and it will show all possible completions.
thats it for now. i got lot of insight from Brock Noland from bashcurescancer.com
PS: i have tested this on GNU bash, version 3.2.0 not sure how things work on other shells. But i guess that should not be difficult to patch it and make it work
good byeee.
Happy sshing…

1 comment March 20, 2008


Archives

 

March 2008
M T W T F S S
« Dec    
 12
3456789
10111213141516
17181920212223
24252627282930
31  

mybloglog rr

Blog Stats