Skip to content

[doc] bash completion #35

@sbonnegent

Description

@sbonnegent

We use this to do bash completion on sshgate command, maybe can be usefull:

We define 2 alias in .bash_aliases:

alias sshgate="ssh -Xt sshgate@YOUR_SERVER"
alias update_sshgate="ssh sshgate@YOUR_SERVER cmd list targets > ~/.sshgate_list_targets.txt"

And in your .bashrc:

_sshgate_hosts()
{
local args cur opts
COMPREPLY=()

argc : parameters number

argc=${COMP_CWORD}

cur : current word

cur="${COMP_WORDS[argc]}"

targets list

opts=""

if [ ! -e ~/.sshgate_list_targets.txt ]
then
/usr/bin/ssh sshgate@YOUR_SERVER cmd list targets > ~/.sshgate_list_targets.txt
fi

for i in $(cat ~/.sshgate_list_targets.txt)
do
opts="$opts $i"
done

we search $cur in targets list

COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
}
complete -F _sshgate_hosts sshgate

An example:

cat ~/.sshgate_list_targets.txt
root@server1
root@server2
root@server3

sshg[TAB]
sshgate [TAB]
sshgate root@server[TAB][TAB]
root@server1
root@server2
root@server3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions