使用 ProxyCommand 和 netcat 模式时,让 ssh 从配置中解析主机名

Jos*_*orn 16 ssh proxy

我正在尝试为弹跳 ssh 连接设置一些通用选项。这是我的~/.ssh/config文件,缩写:

Host *%via
  ProxyCommand ssh gateway -W $(echo %h | cut -d%% -f1) %p

Host gateway
  HostName gateway.example.com
  User username
  ForwardAgent yes
  IdentityFile keypathg

Host target
  User username
  HostName target.example.com
  IdentityFile keypatht
Run Code Online (Sandbox Code Playgroud)

当我利用*%via利用Host别名,我得到:

% ssh -vvv target%via
OpenSSH_5.9p1, OpenSSL 0.9.8y 5 Feb 2013
debug1: Reading configuration data /Users/myuser/.ssh/config
debug1: /Users/myuser/.ssh/config line 5: Applying options for *
debug1: /Users/myuser/.ssh/config line 12: Applying options for *%via
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: auto-mux: Trying existing master
debug1: Control socket "/Users/myuser/.ssh/tmp/target%via_22_myuser" does not exist
debug2: ssh_connect: needpriv 0
debug1: Executing proxy command: exec ssh -A gateway -W $(echo target%via | cut -d% -f1):22
debug1: permanently_drop_suid: 501
debug1: identity file /Users/myuser/.ssh/id_rsa type -1
debug1: identity file /Users/myuser/.ssh/id_rsa-cert type -1
debug1: identity file /Users/myuser/.ssh/id_dsa type -1
debug1: identity file /Users/myuser/.ssh/id_dsa-cert type -1
ssh_exchange_identification: Connection closed by remote host
Run Code Online (Sandbox Code Playgroud)

但是,如果我利用

% ssh target.example.com%via
Run Code Online (Sandbox Code Playgroud)

我击中了目标服务器,但作为错误的用户并且没有公钥身份验证。

在这一点上,我认为我的问题是这种弹跳方法是否在使用时ForwardAgent通过我的 ssh 配置/环境整个,或者只是密钥。如果只是钥匙,前者可以以某种方式使用吗?

我的ssh版本是5.9v1,网关是5.9v1,目标是5.3p1。我相信-W是在 5.4 中引入的,但这对于最后一个盒子来说应该无关紧要吗?利用旧学校nc似乎没有什么不同。

我已经验证我可以手动 ssh 到该行中的每个框。这样做表示没有传递主机名别名信息,就像在网关上一样,我不能,ssh target但我可以ssh target.example.com。这适用于公钥身份验证。顺便说一句,网关和目标具有相同的用户名,这就是为什么如果没有推送配置,这会起作用。

如果ForwardAgent或类似的配置无法推送此信息,绕过它的最明智的方法是在网关上使用此信息保留 .ssh/config 吗?

Hef*_*zen 14

哇,谢谢你问这个问题。我发现很少有人充分利用 SSH,这个问题涉及几个方面。

这不是ProxyCommand问题。在ProxyCommand简单的指示本地ssh客户端试图说服到远程客户端之前做准备的东西。是的,在我们的例子中,我们与另一个 ssh 会话交谈,但是那个会话-W只是接收我们的输入并将其转发到另一台机器。您可以认为准备 ssh 会话是完全独立的。不可避免的汽车类比:您的汽车是同一辆车,无论您是否必须乘坐渡轮才能从 A 点到达 B 点。

这不是ForwardAgent问题。 ForwardAgent让本地客户端提供一种工具,使本地密钥在远程会话的环境中可用。您还没有建立远程会话。

.ssh/config格式问题。请注意第二和第三行 debug1。它们列出了从您的.ssh/config. 您注意到这$ ssh target.example.com%via可行,但因为用户名和密钥错误。好吧,Host target没有读取for 的节(它将提供正确的用户名和密钥文件)。使用了哪些节? **%via

如何让这些选项通过?好吧,很有趣,通配符匹配长度为 0 的字符串。 Host target*将匹配targettarget%viatarget.example.comtarget.example.com%via

所以你问这个问题,.ssh/configgateway机器上设置一个会有所帮助。不,不会。它永远不会被阅读。一切都在我们的本地机器上发生。

我已经解释过,只回答为什么$ ssh target.example.com%via不起作用。

你更喜欢$ ssh target%via. 没错,这样更方便。简短形式失败,因为target找不到主机名;它没有解决。为什么不是 ssh 喷涌:ssh: Could not resolve hostname target: Name or service not known?因为ProxyCommand已经成功建立了。ssh 连接的元素已经建立,但主机名失败发生在它意想不到的地方,所以它用更通用的消息轰炸。我会就此提交一份错误报告,以帮助确定可以改进调试信息的地方。

最后评论:

我喜欢Host *%via语法。它很干净,但很灵活。我以前见过Host *+*,它使用 的第一部分和最后一部分%h(ost)来确定去哪里。但是需要更多的努力来解决这个问题。链接:http : //wiki.gentoo.org/wiki/SSH_jump_host