[doc] bash completion
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
hi i'm use auto completion for sshgate
in your .bashrc
gatehost=sshg 'cmd list targets' | tr -d $'\r' | sed 's/root@//g' 2>&1
complete -W "$gatehost" sshg
complete -o nospace -F _scp -W "$gatehost" scpg