blob: fd5c6cd5fe8a796522e5ca0aca65d6fabe2d073f [file] [log] [blame]
Eitan Adler3fc60b52012-04-17 00:24:58 -04001#!/usr/bin/env bash
Andy Shene0d85822010-12-04 11:03:33 +11002# Bash completion support for ssh.
3
4export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/}
5
6_sshcomplete() {
John O'Gara3b536232014-01-04 12:50:45 +00007 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 Baumann13edf322010-12-06 16:01:06 +010014
15 # parse all defined hosts from .ssh/config
16 if [ -r $HOME/.ssh/config ]; then
John O'Gara3b536232014-01-04 12:50:45 +000017 COMPREPLY=($(compgen -W "$(grep ^Host $HOME/.ssh/config | awk '{print $2}' )" ${OPTIONS}) )
Andy Shene0d85822010-12-04 11:03:33 +110018 fi
Florian Baumann13edf322010-12-06 16:01:06 +010019
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'Gara3b536232014-01-04 12:50:45 +000023 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 Baumann13edf322010-12-06 16:01:06 +010024 fi
25 fi
John O'Gara3b536232014-01-04 12:50:45 +000026
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 Baumann13edf322010-12-06 16:01:06 +010031
32 return 0
Andy Shene0d85822010-12-04 11:03:33 +110033}
34
35complete -o default -o nospace -F _sshcomplete ssh
John O'Gara3b536232014-01-04 12:50:45 +000036