#!/usr/bin/env bash # Bash completion support for ssh. export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/} _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 defined hosts from all ssh configuration files (taking "Include" directives into consideration). # The solution relies on ssh printing the configuration files paths in verbose mode. # # We try to connect to localhost on port 4, which is reserved and should be unused. # In case there is an ssh server listening on that port, we ask ssh to "do nothing" by executing "echo" remotely. config_files="$(ssh -v 127.0.0.1 -p 4 echo 2>&1 | sed -n '/Reading configuration data/ s/.*Reading configuration data \([^\r]*\).*/\1/gp')" for file in $config_files do COMPREPLY=(${COMPREPLY[@]} $(compgen -W "$(grep ^Host "${file}" | awk '{for (i=2; i<=NF; i++) print $i}' )" ${OPTIONS}) ) done # 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" | grep -v ^\| | cut -d, -f 1 | sed -e 's/\[//g' | sed -e 's/\]//g' | cut -d: -f1 | grep -v ssh-rsa)" ${OPTIONS}) ) 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 '{for (i=2; i<=NF; i++) print $i}' )" ${OPTIONS}) ) fi return 0 } complete -o default -o nospace -F _sshcomplete ssh scp