Skip to content

Instantly share code, notes, and snippets.

@hobodave
Created July 14, 2014 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hobodave/3cf64c4d7be111fe411f to your computer and use it in GitHub Desktop.
Save hobodave/3cf64c4d7be111fe411f to your computer and use it in GitHub Desktop.
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 all defined hosts from .ssh/config
if [ -r $HOME/.ssh/config ]; then
COMPREPLY=($(compgen -W "$(grep ^Host $HOME/.ssh/config | awk '{print $2}' )" ${OPTIONS}) )
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)" ${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 '{print $2}' )" ${OPTIONS}) )
fi
return 0
}
complete -o default -o nospace -F _sshcomplete ssh
@hobodave
Copy link
Author

Without Username

|ruby-2.1.2| gm25795-2 in ~
○ → ssh dealestate-app1<TAB><TAB>
dealestate-app1-staging.snc1  dealestate-app1-uat.snc1      dealestate-app1.snc1

|ruby-2.1.2| gm25795-2 in ~
○ → ssh dealestate-app1

Works as expected

With Username

|ruby-2.1.2| gm25795-2 in ~
○ → ssh user@dealestate-app1<TAB>

Pressing TAB here replaces the current prompt and user input with the following

|ruby-2.1.2| gm25795-2 in ~
○ → ssh useruser@dealestate-app1

Pressing TAB a second time, replaces again with:

|ruby-2.1.2| gm25795-2 in ~
○ → ssh useruseruseruser@dealestate-app1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment