在~/.ssh/config
或ssh_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
,但我找不到该%
变量。
(是的!我知道递归的风险。这已在脚本中解决。)
更新:肯斯特指出我可以只需将 Host 值硬编码为脚本的参数即可。当然,这在我给出的示例中是可行的,但如果我对 使用了模式匹配,那么它将不起作用Host
。
答案1
我不确定它是在哪个版本中添加的,但我认为您想使用%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).
答案2
看来您已经有了别名。这有什么问题?
Host some_host_alias
[...]
LocalCommand some_script.sh some_host_alias