[Bash-completion-devel] ssh2 completion func

Peled, Ofer ofer.peled at intel.com
Sun Jan 6 14:31:10 UTC 2013


Hey,
I hope it's not just because I've missed it, but I was recently trying to find a completion script for ssh2 and I failed to do so.

I tried writing one, but I seem to have a problem with the '@' char escaping for the username, now sure I could use the -l command line switch but it's less convenient.

This is what I came up with in the end, it works as long as you don't hit tab too many times, otherwise it duplicates the username :(

If it helps someone, enjoy it.
If you know how to fix the problem, thank you.
If it's already solved and I'm wasting your time for failing to locate this in the repository, shame on me.

The problem with the username
Say I try:
#ssh of<tab>
It completes to:
#ssh ofer\@
The '\' is added on purpose for escaping. But hitting tab once more (to see the options available) will remove it and when I'll try adding in the server name:
#ssh ofer at a<tab>
I'll see :
#ssh oferofer at a


_ssh()
{
    local cur prev
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]};
    prev=${COMP_WORDS[COMP_CWORD-1]};
    SSH_ARR=()
    pos=0

    for F in ~/.ssh2/hostkeys/*
    do
                #i have 2 user names here so there is an if I've removed.
        F=$( /usr/bin/basename ${F} )
        F1=$( echo ${F} | sed -e s/,.*//g | sed -e s/key_.*_/${USER}\\\\\\\\@/g | sed -e 's/.pub//g ')
        SSH_ARR[$pos]=${F1}
        let "pos += 1"
       # also if you want a pick, with the username or without uncomment the lines bellow
        #F2=$( echo ${F} | sed -e s/,.*//g | sed -e s/key_.*_/${USER}\\\\\\\\@/g | sed -e 's/.pub//g ')
        #SSH_ARR[$pos]=${F2}
        #let "pos += 1"

    done

    if [[ '$cur' == '' ]] ; then
        #on empty string return all the options
        COMPREPLY=($(echo "${SSH_ARR[@]}" | tr ',' ' '))
        return 0;
    fi

    if [[ "$cur" == "${USER}\\@" ]] ; then
       #if it's just the username sent, everything else is an option.
        COMPREPLY=($(echo "${SSH_ARR[@]}" | tr ',' ' '))
        return 0;
    fi

    SSH_OPT_STR=$(echo "${SSH_ARR[@]}" | tr ',' ' ')
    COMPREPLY=( $(compgen -v -W "$SSH_OPT_STR"  -- "${cur}") )
    if [[ ${#COMPREPLY[@]} == 0 ]] ; then
        SSH_OPT_STR=$(echo $SSH_OPT_STR | sed -e s/\\\\//g )
        COMPREPLY=( $(compgen -v -W "$SSH_OPT_STR"  -- "${cur}") )
    fi
}
complete -F _ssh ssh

cheers,
Ofer
---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.alioth.debian.org/pipermail/bash-completion-devel/attachments/20130106/2d7e2c09/attachment.html>


More information about the Bash-completion-devel mailing list