求教这段 shell 命令

2017-11-03 11:17:48 +08:00
 warcraft1236

ssh -o ProxyCommand='ssh -A root@111.111.111.111 -W %h:%p' -vNL 4006:192.168.4.48:4006 production

如上所示的命令,我大概理解这是在做什么

有一个疑问就是,-W 参数后边的 %h 和%p 是变量吗?都是从哪取得值?

3315 次点击
所在节点    程序员
17 条回复
LoliconInside
2017-11-03 11:20:52 +08:00
这个是 OpenSSH 的内置变量。以下内容摘自官方 man 手册:

ControlPath
Specify the path to the control socket used for connection sharing as described in the ControlMaster section above or the string ''none'' to disable connection sharing. In the path, '%l' will be substituted by the local host name, '%h' will be substituted by the target host name, '%p' the port, and '%r' by the remote login username. It is recommended that any ControlPath used for opportunistic connection sharing include at least %h, %p, and %r. This ensures that shared connections are uniquely identified.

https://linux.die.net/man/5/ssh_config
alvie
2017-11-03 11:21:55 +08:00
```
man ssh
man ssh_config
```
warcraft1236
2017-11-03 11:30:09 +08:00
@LoliconInside 多谢。在这个命令中,%h 就是 111.111.111.111 %p 就是 22 对吗?
lxf1992521
2017-11-03 12:11:17 +08:00
%h 表示 production,%p 表示 production 主机的端口号,默认为 22
Tink
2017-11-03 12:14:41 +08:00
warcraft1236
2017-11-03 12:16:53 +08:00
@lxf1992521 为啥会是 production 的呢?-W 不是包在 -o ProxyCommand 这个里边吗?
lxf1992521
2017-11-03 12:24:05 +08:00
主机 顺序:A -> B -> C
命令:ssh -oProxyCommand="ssh -q -W %h:%p B" C

1. 首先 A 成功登录到 B ;
2. 让 B 帮忙打通一条到 C 的通道,A 可以通过这条通道直达 C ;
3. A 通过这条通道,可以直接登录 C,认证也是 A 与 C 之间的认证,没有 B 的事情;
4. B 到 C 之间的通道只是一个 TCP 协议层的数据包转发,没有任何 SSH 协议的事情;
zyp0921
2017-11-03 14:01:53 +08:00
看不懂 看不懂
warcraft1236
2017-11-03 14:14:29 +08:00
@lxf1992521 这下有点不太明白 -W -A 这个参数的作用了。
-A 这个参数是说开启 B 的代理吗?-W 是让 A 的 stdin stdout 都通过代理转发到 %h %p 这个地址上吗?
lxf1992521
2017-11-03 14:48:36 +08:00
-A 参数和这边的 TCP 传输层端口转发没有关系,-A 是 SSH 协议层面的认证代理转发,可有可无;
warcraft1236
2017-11-03 15:25:00 +08:00
@lxf1992521 我试了一下,如果没有-A 这个参数,是会出错的
congeec
2017-11-04 03:53:56 +08:00
warcraft1236
2017-11-04 22:29:13 +08:00
@congeec 他没有解决我的问题啊。并没有解释清楚 proxycommand 里边的各个参数的意义
congeec
2017-11-05 00:52:17 +08:00
@warcraft1236 我发的链接你都看了?
cxbig
2017-11-05 05:00:12 +08:00
第一段的-o 参数是 Jump Host,通过堡垒机登陆内网服务器。
第二段是 SSH 通道,把远程服务器的 4006 端口映射到本地。
最后那个 production 是本地用户~/.ssh/config 里定义好的服务器别名( alias )
Rheinmetal
2017-11-05 09:47:50 +08:00
这个不是 shell 的问题 是 OpenSSH 的问题
建议阅读 SSH The Secure Shell 第七章 和 第九章
https://docstore.mik.ua/orelly/networking_2ndEd/ssh/index.htm
warcraft1236
2017-11-05 22:06:49 +08:00
@congeec 对啊,我不是不懂整个命令干了什么事情,我是搞不明白-a 和-w 这两个参数在里边具体起到了什么作用,我看了 man ssh 中对着两个参数的解释,但是没有搞明白在我发的这个命令中,这两个参数都干了什么

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/403187

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX