bash domain completion
am 05.10.2007 21:15:01 von Heruan
I have a resolv.conf with a search on some domains:
search australia.domain.tld brasil.domain.tld china.domain.tld ...
I would like to make bash complete those search domains so that
# ssh host.a[TAB]
becomes
# ssh host.australia.domain.tld
and
# ssh host.b[TAB]
becomes
# ssh host.brasil.domain.tld
How can I achieve this?
Thank you!
H.
Re: bash domain completion
am 06.10.2007 00:47:52 von Heruan
Heruan wrote:
> I have a resolv.conf with a search on some domains:
>
> search australia.domain.tld brasil.domain.tld china.domain.tld ...
>
> I would like to make bash complete those search domains so that
>
> # ssh host.a[TAB]
>
> becomes
>
> # ssh host.australia.domain.tld
[cut]
With some useful help in manipulating strings, I got this:
_domains()
{
local cur domains
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
search=`grep 'search' /etc/resolv.conf | cut -d' ' -f2-`
if [[ ${cur} == *.* ]]; then
host=${cur/.*/}
domains="$(for i in $search; do echo $host.$i; done \
| paste -s -d" " -)"
COMPREPLY=( $(compgen -W "${domains}" -- ${cur}) )
return 0
fi
}
complete -F _domains ssh
complete -F _domains ping
It's a bit more complicated to get it work if we have an host on a
subdomain not listed on resolv.conf...
Heruan