Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,000 hints and counting!


Click here to return to the 'Create on-the-fly hostname lists for ssh tab completion' hint
The following comments are owned by whomever posted them. This site is not responsible for what they say.
Create on-the-fly hostname lists for ssh tab completion
Authored by: leono on Mon, Mar 24 2008 at 11:05AM PDT
Nice job, Nem! I don't actually have a ~/.ssh/config file, so here's a modification to deal with that situation:

_complete_ssh_hosts ()
{
        COMPREPLY=()
        cur="${COMP_WORDS[COMP_CWORD]}"
        comp_ssh_hosts=`cat ~/.ssh/known_hosts | \
                        cut -f 1 -d ' ' | \
                        sed -e s/,.*//g | \
                        grep -v ^# | \
                        uniq | \
                        grep -v "\[" ;
                if [ -f ~/.ssh/config ]; then
                        cat ~/.ssh/config | \
                                grep "^Host " | \
                                awk '{print $2}'
                fi
                `
        COMPREPLY=( $(compgen -W "${comp_ssh_hosts}" -- $cur))
        return 0
}
complete -F _complete_ssh_hosts ssh


[ Reply to This | # ]
Create on-the-fly hostname lists for ssh tab completion
Authored by: slswift on Mon, Mar 24 2008 at 7:34PM PDT
Thanks for the slick inclusion of .ssh/config. However, I had to make a quick tweak to 'awk' out the 3rd entry rather than the 2nd.

awk '{print $3}'

My .ssh/config uses this syntax, which may or may not be 'standard', but definitely works. My config syntax comes from ssh v. 1, so the current accepted syntax could be VASTLY different.

Host = solutions


[ Reply to This | # ]