如何获取〜/ .ssh / config中的主机值

~/.ssh/configssh_config文件中, %h会给你HostName值,但是如何得到Host (“别名”)的值呢?

我为什么要这样做? 那么,这里是一个例子

  Host some_host_alias HostName 1.2.3.4 User my_user_name PasswordAuthentication no IdentityFile ~/.ssh/some_host_alias.rsa.id LocalCommand some_script.sh %h # <---- this is the critical line 

如果我将%h传递给脚本,那么它会使用1.2.3.4,这将无法提供连接到该计算机所需的全部选项。 我需要传递some_host_alias ,但我找不到%variables。

(和:是的!我意识到recursion的风险,在脚本里面解决了。)


更新:Kenster指出,我可以硬编码主机值作为脚本的参数。 当然,这将在我给的例子中工作,但如果我使用模式匹配Host ,它将无法正常工作。

我不确定它添加了哪个版本,但是我认为你想用%n代替%h 。 从ssh_config(5)手册页:

 LocalCommand Specifies a command to execute on the local machine after successfully connecting to the server. The command string extends to the end of the line, and is executed with the user's shell. The following escape character substitutions will be performed: '%d' (local user's home directory), '%h' (remote host name), '%l' (local host name), '%n' (host name as provided on the command line), '%p' (remote port), '%r' (remote user name) or '%u' (local user name). 

看来你已经有别名了。 这有什么问题?

 Host some_host_alias [...] LocalCommand some_script.sh some_host_alias