Andy Shen | e0d8582 | 2010-12-04 11:03:33 +1100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Bash completion support for ssh. |
| 3 | |
| 4 | export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/} |
| 5 | |
| 6 | _sshcomplete() { |
Florian Baumann | 13edf32 | 2010-12-06 16:01:06 +0100 | [diff] [blame] | 7 | |
| 8 | # parse all defined hosts from .ssh/config |
| 9 | if [ -r $HOME/.ssh/config ]; then |
| 10 | COMPREPLY=($(compgen -W "$(grep ^Host $HOME/.ssh/config | awk '{print $2}' )" -- ${COMP_WORDS[COMP_CWORD]})) |
Andy Shen | e0d8582 | 2010-12-04 11:03:33 +1100 | [diff] [blame] | 11 | fi |
Florian Baumann | 13edf32 | 2010-12-06 16:01:06 +0100 | [diff] [blame] | 12 | |
| 13 | # parse all hosts found in .ssh/known_hosts |
| 14 | if [ -r $HOME/.ssh/known_hosts ]; then |
| 15 | if grep -v -q -e '^ ssh-rsa' $HOME/.ssh/known_hosts ; then |
Nils Winkler | 9df9ac3 | 2011-10-25 08:57:39 +0200 | [diff] [blame] | 16 | 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]} )) |
Florian Baumann | 13edf32 | 2010-12-06 16:01:06 +0100 | [diff] [blame] | 17 | fi |
| 18 | fi |
| 19 | |
| 20 | return 0 |
Andy Shen | e0d8582 | 2010-12-04 11:03:33 +1100 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | complete -o default -o nospace -F _sshcomplete ssh |