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: BobHarris on Mar 24, '08 09:05:54PM
I do not know how to directly make completion re-evaluate known_hosts for every TAB, but maybe this is the next best thing.

If you put the following code in your .bashrc (or .bash_profile; where ever you setup your PS1 prompt variable), then after each command when the prompt is output, the known_hosts file will be checked to see if has been modified, and if so, it will be re-evaluated:

update_hosts_complete()
{
    typeset ref=/tmp/hosts_complete.$$
    typeset khosts=$HOME/.ssh/known_hosts

    if [[ $khosts -nt $ref ]]; then
        touch $ref
        complete -W "$(awk -F'[, ]' '
            $1 !~ /[/ {hosts[$1]=1}
            END{
                for(host in hosts)
                    printf(" %s", host)
            }' $khosts)" ssh
    fi
}
PS1="$(update_hosts_complete) your prompt goes here> "

Bob Harris

[ Reply to This | # ]