Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,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 whoever posted them. This site is not responsible for what they say.
Create on-the-fly hostname lists for ssh tab completion
Authored by: Nem on Mar 24, '08 09:34:23AM
The macports suggestion is a good one, but IMO, includes too much. If you don't have macports installed, you can add the following to your .bash_profile (I wouldn't add it to my .bashrc, since that gets read in by cron jobs and remote commands).

There are a couple improvements here. First, this will also read in aliases in your ~/.ssh/config file. Secondly, it will ignore commented out entries in your ~/.ssh/known_hosts file. Finally, this is a function and not a static list. Thus, it is immediately aware of any new additions to either file (although on really slow machines it will be slower than a static list).


_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 "\[" ;
                cat ~/.ssh/config | \
                        grep "^Host " | \
                        awk '{print $2}'
                `
        COMPREPLY=( $(compgen -W "${comp_ssh_hosts}" -- $cur))
        return 0
}
complete -F _complete_ssh_hosts ssh


---
Nem W. Schlecht
http://geekmuse.net/

[ Reply to This | # ]

Create on-the-fly hostname lists for ssh tab completion
Authored by: leono on Mar 24, '08 11:05:40AM
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 Mar 24, '08 07:34:52PM

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 | # ]
Create on-the-fly hostname lists for ssh tab completion
Authored by: ssevenup on Mar 24, '08 06:37:29PM

tcsh and/or zsh versions? I actually would find the aliases in config more useful than known_hosts I think? My user name is rarely the same on the box I'm logging into.

---
Mark Moorcroft
ELORET Corp. - NASA/Ames RC
Sys. Admin.



[ Reply to This | # ]