常规的 ~/.ssh/config ,例如:
Host bj.* # 北京内网测试
Port 2009
User root
IdentityFile ~/.ssh/bj.admin.git.key
Host bj.201 # Hadoop.201
HostName 10.0.30.201
#<
[email protected]>
Host bj.202 # Hadoop.202
HostName 10.0.30.202
Host bj.203 # Hadoop.203
HostName 10.0.30.203
然后配置环境(如 ~/.bashrc )
_complete_liao_hosts_ex() {
local curw=${COMP_WORDS[COMP_CWORD]}
local wordlist=$(awk '{if ($1=="Host") if ($4=="") print $2; else printf "%s::%s\n",$2,$4}' ~/.ssh/config)
COMPREPLY=($(compgen -W '${wordlist[@]}' -- "$curw"))
return 0
}
function lsshl() {
n=$1
ssh ${n%%:*}
}
complete -F _complete_liao_hosts_ex lsshl
之后,在 terminal 中,输入 lsshl bj. 再按 tab 补全,就会发现:
$ lsshl bj.
bj.*::北京内网测试 bj.191 bj.191.2 bj.201::Hadoop.201 bj.202::Hadoop.202 bj.203::Hadoop.203 bj.98 bj.98.2
so easy