[Bash-completion-devel] ssh2 completion func
Igor Murzov
intergalactic.anonymous at gmail.com
Wed Jan 9 15:14:20 UTC 2013
On Sun, 6 Jan 2013 14:31:10 +0000
"Peled, Ofer" <ofer.peled at intel.com> wrote:
> 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.
Is it an alias for `ssh -2` ? If yes, then you can use completions/ssh, which
is distributed with bash-completion.
> 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.
You can look at implementation of _user_at_host() to learn how to fix your issue:
http://anonscm.debian.org/gitweb/?p=bash-completion/bash-completion.git;a=blob;f=bash_completion;hb=HEAD#l1362
> 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
>
More information about the Bash-completion-devel
mailing list