Skip to content

Instantly share code, notes, and snippets.

@sontek
Created July 12, 2013 17:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sontek/5986226 to your computer and use it in GitHub Desktop.
Save sontek/5986226 to your computer and use it in GitHub Desktop.
autocomplete ssh via /etc/hosts and ssh_config
_complete_hosts () {
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
host_list=`{
for c in /etc/ssh_config /etc/ssh/ssh_config ~/.ssh/config
do [ -r $c ] && sed -n -e 's/^Host[[:space:]]//p' -e 's/^[[:space:]]*HostName[[:space:]]//p' $c
done
for k in /etc/ssh_known_hosts /etc/ssh/ssh_known_hosts ~/.ssh/known_hosts
do [ -r $k ] && egrep -v '^[#\[]' $k|cut -f 1 -d ' '|sed -e 's/[,:].*//g'
done
sed -n -e 's/^[0-9][0-9\.]*//p' /etc/hosts; }|tr ' ' '\n'|grep -v '*'`
COMPREPLY=( $(compgen -W "${host_list}" -- $cur))
return 0
}
complete -F _complete_hosts ssh
complete -F _complete_hosts host
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment