I want to create bash autocomplete for ssh. I have an a large network, with about 10 level 1 domains, each containts about 20 2 level domains, it can contains level 3 domains or network equipment.
All data stored in my DNS and I can transfer zone with dig, and parse output to ssh_config. For understandong I have records similar to this:
Host ZONE-A/City1-Name/RouterName
Hostname AAA
Host ZONE-A/City2-Name/RouterName
Hostname BBB
Host ZONE-B/City3/RouterName
Hostname CCC
Host ZONE-B/City4/RouterName
Hostname DDD
Host ZONE-C/City5/RouterName
Hostname EEE
Host ZONE-C/City6/RouterName
Hostname FFF
Host ZONE-C/City6/District1/RouterName
Hostname GGG
I'm tryed to parse this and send to COMPREPLY only substring with this code:
_ssh()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=$(grep '^Host' /etc/ssh/ssh_config | grep -v '[?*]' | cut -d ' ' -f 2-)
COMPREPLY=( $(compgen -W "$opts" -- ${cur} \
| awk -v poscount=$(echo $cur \
| awk -F\/ '{print NF}') -F\/ '{print $poscount "/"}' | uniq) )
return 0
}
complete -F _ssh ssh
but when i trying to
ssh ZONE-A/City1 <TAB>
it changes to
ssh City1-Name
And autocomplete dont working.
If I'm trying to not substitute strings as here, and use
-o filenames
autocomplete shows only end devices names (RouterName)
Is there any way of doing this in bash?