Added parsing of /etc/hosts and support for username@host completion

pull/255/head
John O'Gara 2014-01-04 12:50:45 +00:00
parent 3f41442e3a
commit 3b53623f64
1 changed files with 15 additions and 2 deletions

View File

@ -4,20 +4,33 @@
export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/} export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/}
_sshcomplete() { _sshcomplete() {
local CURRENT_PROMPT="${COMP_WORDS[COMP_CWORD]}"
if [[ ${CURRENT_PROMPT} == *@* ]] ; then
local OPTIONS="-P ${CURRENT_PROMPT/@*/}@ -- ${CURRENT_PROMPT/*@/}"
else
local OPTIONS=" -- ${CURRENT_PROMPT}"
fi
# parse all defined hosts from .ssh/config # parse all defined hosts from .ssh/config
if [ -r $HOME/.ssh/config ]; then if [ -r $HOME/.ssh/config ]; then
COMPREPLY=($(compgen -W "$(grep ^Host $HOME/.ssh/config | awk '{print $2}' )" -- ${COMP_WORDS[COMP_CWORD]})) COMPREPLY=($(compgen -W "$(grep ^Host $HOME/.ssh/config | awk '{print $2}' )" ${OPTIONS}) )
fi fi
# parse all hosts found in .ssh/known_hosts # parse all hosts found in .ssh/known_hosts
if [ -r $HOME/.ssh/known_hosts ]; then if [ -r $HOME/.ssh/known_hosts ]; then
if grep -v -q -e '^ ssh-rsa' $HOME/.ssh/known_hosts ; then if grep -v -q -e '^ ssh-rsa' $HOME/.ssh/known_hosts ; then
COMPREPLY=( ${COMPREPLY[@]} $(compgen -W "$( awk '{print $1}' $HOME/.ssh/known_hosts | cut -d, -f 1 | sed -e 's/\[//g' | sed -e 's/\]//g' | cut -d: -f1 | grep -v ssh-rsa)" -- ${COMP_WORDS[COMP_CWORD]} )) COMPREPLY=( ${COMPREPLY[@]} $(compgen -W "$( awk '{print $1}' $HOME/.ssh/known_hosts | cut -d, -f 1 | sed -e 's/\[//g' | sed -e 's/\]//g' | cut -d: -f1 | grep -v ssh-rsa)" ${OPTIONS}) )
fi fi
fi fi
# parse hosts defined in /etc/hosts
if [ -r /etc/hosts ]; then
COMPREPLY=( ${COMPREPLY[@]} $(compgen -W "$( grep -v '^[[:space:]]*$' /etc/hosts | grep -v '^#' | awk '{print $2}' )" ${OPTIONS}) )
fi
return 0 return 0
} }
complete -o default -o nospace -F _sshcomplete ssh complete -o default -o nospace -F _sshcomplete ssh