Eitan Adler | 3fc60b5 | 2012-04-17 00:24:58 -0400 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Andy Shen | e0d8582 | 2010-12-04 11:03:33 +1100 | [diff] [blame] | 2 | # Bash completion support for ssh. |
| 3 | |
| 4 | export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/} |
| 5 | |
| 6 | _sshcomplete() { |
John O'Gara | 3b53623 | 2014-01-04 12:50:45 +0000 | [diff] [blame] | 7 | local CURRENT_PROMPT="${COMP_WORDS[COMP_CWORD]}" |
| 8 | if [[ ${CURRENT_PROMPT} == *@* ]] ; then |
| 9 | local OPTIONS="-P ${CURRENT_PROMPT/@*/}@ -- ${CURRENT_PROMPT/*@/}" |
| 10 | else |
| 11 | local OPTIONS=" -- ${CURRENT_PROMPT}" |
| 12 | fi |
| 13 | |
Florian Baumann | 13edf32 | 2010-12-06 16:01:06 +0100 | [diff] [blame] | 14 | |
| 15 | # parse all defined hosts from .ssh/config |
| 16 | if [ -r $HOME/.ssh/config ]; then |
John O'Gara | 3b53623 | 2014-01-04 12:50:45 +0000 | [diff] [blame] | 17 | COMPREPLY=($(compgen -W "$(grep ^Host $HOME/.ssh/config | awk '{print $2}' )" ${OPTIONS}) ) |
Andy Shen | e0d8582 | 2010-12-04 11:03:33 +1100 | [diff] [blame] | 18 | fi |
Florian Baumann | 13edf32 | 2010-12-06 16:01:06 +0100 | [diff] [blame] | 19 | |
| 20 | # parse all hosts found in .ssh/known_hosts |
| 21 | if [ -r $HOME/.ssh/known_hosts ]; then |
| 22 | if grep -v -q -e '^ ssh-rsa' $HOME/.ssh/known_hosts ; then |
John O'Gara | 3b53623 | 2014-01-04 12:50:45 +0000 | [diff] [blame] | 23 | 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}) ) |
Florian Baumann | 13edf32 | 2010-12-06 16:01:06 +0100 | [diff] [blame] | 24 | fi |
| 25 | fi |
John O'Gara | 3b53623 | 2014-01-04 12:50:45 +0000 | [diff] [blame] | 26 | |
| 27 | # parse hosts defined in /etc/hosts |
| 28 | if [ -r /etc/hosts ]; then |
| 29 | COMPREPLY=( ${COMPREPLY[@]} $(compgen -W "$( grep -v '^[[:space:]]*$' /etc/hosts | grep -v '^#' | awk '{print $2}' )" ${OPTIONS}) ) |
| 30 | fi |
Florian Baumann | 13edf32 | 2010-12-06 16:01:06 +0100 | [diff] [blame] | 31 | |
| 32 | return 0 |
Andy Shen | e0d8582 | 2010-12-04 11:03:33 +1100 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | complete -o default -o nospace -F _sshcomplete ssh |
John O'Gara | 3b53623 | 2014-01-04 12:50:45 +0000 | [diff] [blame] | 36 | |