From: Florian Baumann Date: Mon, 6 Dec 2010 15:01:06 +0000 (+0100) Subject: added advanced ssh completion X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=13edf326c450548e14673c82f75c717425d19a8b;p=common%2Fbash_it.git added advanced ssh completion --- diff --git a/completion/ssh.completion.bash b/completion/ssh.completion.bash index 7691333..30fd65d 100644 --- a/completion/ssh.completion.bash +++ b/completion/ssh.completion.bash @@ -4,10 +4,20 @@ export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/} _sshcomplete() { - if [ -f $HOME/.ssh/config ]; then - COMPREPLY=($(compgen -W "`ruby -e"puts open('${HOME}/.ssh/config', 'r') { |f| f.readlines }.find_all { |l| l =~ /^Host/ }.inject([]) { |hosts, line| hosts << line[5..-1].split }.flatten.sort.uniq"`" -- ${COMP_WORDS[COMP_CWORD]})) - return 0 + + # parse all defined hosts from .ssh/config + if [ -r $HOME/.ssh/config ]; then + COMPREPLY=($(compgen -W "$(grep ^Host $HOME/.ssh/config | awk '{print $2}' )" -- ${COMP_WORDS[COMP_CWORD]})) fi + + # parse all hosts found in .ssh/known_hosts + if [ -r $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]} )) + fi + fi + + return 0 } complete -o default -o nospace -F _sshcomplete ssh