dot*_*hen 77 bash ssh scp autocomplete
我配置了一些服务器~/.ssh/config,例如alpha和beta。我如何配置 Bash 以便命令和自动完成已配置服务器的名称?$ ssh alTab$ scp file.tgz alTab
我不想每次添加服务器时都将服务器添加到另一个文件(即 Bash 数组),因为我们定期添加和删除服务器并且列表非常大。
这是在 Kubuntu 12.10 上,我确实安装了bash-completion。
dot*_*hen 84
找到了!!
似乎在 Ubuntu 中,条目~/.ssh/known_hosts是散列的,因此 SSH 完成无法读取它们。这是一个功能,而不是一个错误。即使添加HashKnownHosts no到~/.ssh/config和/etc/ssh/ssh_config我无法防止主机散列。
但是,我感兴趣的主机也在~/.ssh/config. 这是 Bash Completion 的脚本,它从该文件中读取条目:
_ssh()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=$(grep '^Host' ~/.ssh/config ~/.ssh/config.d/* 2>/dev/null | grep -v '[?*]' | cut -d ' ' -f 2-)
COMPREPLY=( $(compgen -W "$opts" -- ${cur}) )
return 0
}
complete -F _ssh ssh
Run Code Online (Sandbox Code Playgroud)
放入该脚本,/etc/bash_completion.d/ssh然后使用以下命令获取它:
$ . /etc/bash_completion.d/ssh
Run Code Online (Sandbox Code Playgroud)
我发现本指南非常宝贵,没有它我就无法编写脚本。感谢Steve Kemp编写了那本了不起的指南!
slm*_*slm 17
您没有说明您使用的是什么发行版,但在我的 Fedora 19 系统上,我安装了以下软件包,bash-completion它通过此完成规则文件提供此功能:
/usr/share/bash-completion/completions/ssh
Run Code Online (Sandbox Code Playgroud)
这是我安装的软件包:
$ rpm -aq |grep completion
bash-completion-2.1-2.fc19.noarch
Run Code Online (Sandbox Code Playgroud)
如果您查看该规则文件,您将看到正在询问该$HOME/.ssh/config文件的节:
$ grep config /usr/share/bash-completion/completions/ssh
local configfile
local -a config
# Search COMP_WORDS for '-F configfile' or '-Fconfigfile' argument
configfile="$(dequote "${1:2}")"
[[ $1 ]] && configfile="$(dequote "$1")"
_known_hosts_real -a -F "$configfile" "$cur"
local configfile
# Search COMP_WORDS for '-F configfile' argument
configfile="$(dequote "${1:2}")"
[[ $1 ]] && configfile="$(dequote "$1")"
_known_hosts_real -a -F "$configfile" "$cur"
local configfile prefix
# Search COMP_WORDS for '-F configfile' or '-Fconfigfile' argument
configfile="$(dequote "${1:2}")"
[[ $1 ]] && configfile="$(dequote "$1")"
_known_hosts_real -c -a -F "$configfile" "$cur"
Run Code Online (Sandbox Code Playgroud)
我还发现了这个 Gist,known_hosts_autocomplete.sh,除了$HOME/.ssh/known_hosts文件之外,它做了类似的事情。
# add to ~/.bash_profile, and close/reopen a shell. Will autocomplete any
# hosts found in known_hosts.
complete -W "$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | \
sed -e s/,.*//g | uniq | grep -v "\["`;)" ssh
Run Code Online (Sandbox Code Playgroud)
$HOME/.ssh/config如果由于某种原因您无法找到ssh已预先打包的完成规则文件,您可以使用您的文件做类似的事情。
M1k*_*1ke 10
我发现自动完成功能不起作用,因为 Ubuntu 对已知主机进行了哈希处理。你可以加
Host *
HashKnownHosts no
Run Code Online (Sandbox Code Playgroud)
到您的.ssh/config文件,但现有主机不会被取消散列。
在 Debian 和 Ubuntu 中启用 ssh 自动完成:
sudo apt-get install bash-completion
Run Code Online (Sandbox Code Playgroud)
请注意,这与 known_hosts 散列没有任何关系,这与上述内容和原始问题相反。如果您想从 known_hosts 自动完成,那么您当然必须禁用散列,但强烈建议您不要这样做。
例如,我有:
Host *
HashKnownHosts yes
Run Code Online (Sandbox Code Playgroud)
在我的 .ssh/config 中,我仍然可以对 .ssh/config 和 /etc/hosts 中列出的主机进行 ssh 自动完成。您确实需要将主机添加到 .ssh/config 如 OP 所述:
主机 my-awesome-host 主机名 the.real.host.name
(或者,您可以将主机条目添加到 /etc/hosts,这是 Debian/Ubuntu 脚本的另一个来源。)
然后,您只需输入即可ssh my-awe<tab>自动完成。同样,即使您使用 HashKnownHosts,这也是强烈推荐的。(请注意,需要在您的 bash shell 中启用 bash 完成功能,并且您需要专门为您的发行版安装上述脚本。)
然后,将这些行添加到您的.bashrc以启用它(需要注销并重新登录,或者只需bash输入新的内容即可启动新的 shell。(如果它已经在/etc/bash.bashrc和/etc/profilesources 中启用,则不需要启用/etc/bash.bashrc)。
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
Run Code Online (Sandbox Code Playgroud)
这将使SSH自动完成(除其他事项外!)从~/.ssh/config,/etc/hosts等等。
请注意,Debian 默认使用 ash 而不是 bash。您可以轻松切换到 bash:
sudo usermod -s /bin/bash "$USER"
Run Code Online (Sandbox Code Playgroud)
(您需要注销并重新登录才能生效。)
在 Ubuntu 14.04 上,ssh自动完成您中提到的服务器~/.ssh/config
当我意识到我经常访问的服务器中只有一台是自动完成的时,我注意到了这一点。两者之间的唯一区别是 ssh 配置文件中与身份验证相关的条目。当我向其他服务器的配置文件添加新条目时,它也开始自动完成。
以下是针对那些提出问题的人的条目:
HOST server-name
GSSAPIAuthentication=no
Run Code Online (Sandbox Code Playgroud)
如果您在配置中指定的内容很重要(当然,只要它仍然有效),我会感到非常惊讶。