V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
warcraft1236
V2EX  ›  程序员

求教这段 shell 命令

  •  
  •   warcraft1236 · 2017-11-03 11:17:48 +08:00 · 3307 次点击
    这是一个创建于 2358 天前的主题,其中的信息可能已经有所发展或是发生改变。

    ssh -o ProxyCommand='ssh -A [email protected] -W %h:%p' -vNL 4006:192.168.4.48:4006 production

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

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

    17 条回复    2017-11-05 22:06:49 +08:00
    LoliconInside
        1
    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
        2
    alvie  
       2017-11-03 11:21:55 +08:00
    ```
    man ssh
    man ssh_config
    ```
    warcraft1236
        3
    warcraft1236  
    OP
       2017-11-03 11:30:09 +08:00
    @LoliconInside 多谢。在这个命令中,%h 就是 111.111.111.111 %p 就是 22 对吗?
    lxf1992521
        4
    lxf1992521  
       2017-11-03 12:11:17 +08:00
    %h 表示 production,%p 表示 production 主机的端口号,默认为 22
    warcraft1236
        6
    warcraft1236  
    OP
       2017-11-03 12:16:53 +08:00
    @lxf1992521 为啥会是 production 的呢?-W 不是包在 -o ProxyCommand 这个里边吗?
    lxf1992521
        7
    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
        8
    zyp0921  
       2017-11-03 14:01:53 +08:00
    看不懂 看不懂
    warcraft1236
        9
    warcraft1236  
    OP
       2017-11-03 14:14:29 +08:00
    @lxf1992521 这下有点不太明白 -W -A 这个参数的作用了。
    -A 这个参数是说开启 B 的代理吗?-W 是让 A 的 stdin stdout 都通过代理转发到 %h %p 这个地址上吗?
    lxf1992521
        10
    lxf1992521  
       2017-11-03 14:48:36 +08:00
    -A 参数和这边的 TCP 传输层端口转发没有关系,-A 是 SSH 协议层面的认证代理转发,可有可无;
    warcraft1236
        11
    warcraft1236  
    OP
       2017-11-03 15:25:00 +08:00
    @lxf1992521 我试了一下,如果没有-A 这个参数,是会出错的
    warcraft1236
        13
    warcraft1236  
    OP
       2017-11-04 22:29:13 +08:00
    @congeec 他没有解决我的问题啊。并没有解释清楚 proxycommand 里边的各个参数的意义
    congeec
        14
    congeec  
       2017-11-05 00:52:17 +08:00
    @warcraft1236 我发的链接你都看了?
    cxbig
        15
    cxbig  
       2017-11-05 05:00:12 +08:00
    第一段的-o 参数是 Jump Host,通过堡垒机登陆内网服务器。
    第二段是 SSH 通道,把远程服务器的 4006 端口映射到本地。
    最后那个 production 是本地用户~/.ssh/config 里定义好的服务器别名( alias )
    Rheinmetal
        16
    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
        17
    warcraft1236  
    OP
       2017-11-05 22:06:49 +08:00
    @congeec 对啊,我不是不懂整个命令干了什么事情,我是搞不明白-a 和-w 这两个参数在里边具体起到了什么作用,我看了 man ssh 中对着两个参数的解释,但是没有搞明白在我发的这个命令中,这两个参数都干了什么
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3740 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 00:52 · PVG 08:52 · LAX 17:52 · JFK 20:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.