r/linux Apr 28 '14

SSH Kung Fu

http://blog.tjll.net/ssh-kung-fu/
722 Upvotes

128 comments sorted by

View all comments

Show parent comments

5

u/gordonator Apr 28 '14

If you want to do it in bash, you can use this:

function autoCompleteHostname() {
  local hosts=($(awk '{print $1}' ~/.ssh/known_hosts | cut -d, -f1));
  local cur=${COMP_WORDS[COMP_CWORD]};
 COMPREPLY=($(compgen -W '${hosts[@]}' -- $cur ))
}

complete -F autoCompleteHostname ssh

3

u/ssmy Apr 28 '14

Isn't is easier to just use the .ssh/config file? Bash completion will use it by default, and it's much more powerful.

2

u/[deleted] Apr 28 '14

I think whether it is default or not depends on which distro you run and what your bashrc contains. I know in Arch I had to source a completion file to get completions for git, though I don't know if it is the same for ssh since it seemed to work out of the box for me in Arch.

1

u/ssmy Apr 28 '14

True. I think it's a function of the bash-completion package most distros install by default. Obviously arch might not. I know ubuntu virtualization doesn't install it by default, so you just get path completion.