简体   繁体   English

SSH通过多跳进行远程转发

[英]ssh remoteforward over multiple hops

I am attempting a multi-hop SSH tunnel that needs to route traffic in both directions. 我正在尝试一个多跳SSH隧道,该隧道需要双向路由流量。 My network config is: 我的网络配置是:

My personal shell is on machineA 我的个人外壳在机器上
machineA can SSH into machineB machineA可以SSH进入machineB
machineB can SSH into machineC machineB可以SSH进入machineC
machineC is locally connected via ethernet to machineD machineC通过以太网本地连接到machineD

There is a service running on machineD wherein machineC sends UDP packets to machineD's portX, and machineD sends its replies via UDP to machineC's portY. 在machineD上运行着一项服务,其中machineC将UDP数据包发送到machineD的portX,而machineD通过UDP将其答复发送到machineC的portY。

I have successfully done the following: 我已成功完成以下操作:
(from machineA) (来自机器A)
ssh machineB SSH机器B
(from resulting shell) (来自生成的shell)
ssh machineC SSH机器C
(from resulting shell) (来自生成的shell)
echo "my command" | 回显“我的命令” | nc -u machineD portX #Send command to machineD's service nc -u machineD portX#发送命令到machineD的服务
nc -ul portY #Read the results on machineC's port nc -ul portY#在machineC的端口上读取结果

I would like to do all of this via tunnels, so that I can run custom scripts directly on machine A to formulate service commands and parse the results. 我想通过隧道完成所有这些操作,以便可以直接在计算机A上运行自定义脚本来制定服务命令并解析结果。 I tried the following in my .ssh config file: 我在.ssh配置文件中尝试了以下操作:

    host machineB
        hostname x.x.x.x
        user username_machineB
        localforward 1234 machineC:22

    host machineC
        hostname localhost
        user username_machineC
        port 1234
        localforward 1235 machineD:portX
        remoteforward 1236 localhost:portY

I thought I could then do the following: 我以为可以做到以下几点:
(from machineA) (来自机器A)
ssh machineB SSH机器B
(from machineA again) (再次来自machineA)
ssh machineC SSH机器C
(from machineA again) (再次来自machineA)
echo "my command" | 回显“我的命令” | nc -u localhost 1235 nc -u localhost 1235
nc -ul 1236 数控-ul 1236

But...it doesn't seem to work. 但是...似乎不起作用。 I don't see any of the expected replies on 1236. I'm not exactly sure how to debug this. 我看不到有关1236的任何预期答复。我不确定如何调试此消息。 I'm also not entirely sure of the format of those "localforward" and "remoteforward" lines on machineC's configuration. 我也不完全确定machineC的配置中“ localforward”和“ remoteforward”行的格式。 I don't know who will be interpreted as "localhost" when evaluating those lines. 我不知道在评估这些行时谁将被解释为“ localhost”。 I suspect that remoteforwarding might be disabled on machineC, but I want to make sure I have configured everything else correctly first. 我怀疑在machineC上可能禁用了远程转发,但是我想确保首先正确配置了所有其他内容。 Am I Doing It Wrong? 我做错了吗?

Alternatively, is there another way to achieve my end goal without having to change any configuration on machineB, C, or D? 或者,是否有另一种方法可以实现我的最终目标,而不必更改machineB,C或D上的任何配置? What I would like to do is use machineA to programatically construct complex commands intended for machineD, and parse the results using scripts on machineA as well. 我想做的是使用machineA以编程方式构造用于machineD的复杂命令,并使用machineA上的脚本来解析结果。

You have to think backwards when you are doing this. 在执行此操作时,您必须向后思考。

So basically machC can talk to machD's portX. 因此,基本上machC可以与machD的portX对话。

So you really want to run this on machA: 因此,您真的想在machA上运行此代码:

ssh machC SSH机器

This is your end goal since that machine sends and receives from machD 这是您的最终目标,因为该机器从machD发送和接收

Now you cannot get to machC directly, this is where your ProxyCommand entries come in. 现在您无法直接进入machC,这是您的ProxyCommand条目所在的位置。

host machC
    ProxyCommand ssh machB nc %h %p

So you said machA can ssh to machB no problem. 因此,您说过machA可以转换为machB没问题。 now if you do: 现在,如果您这样做:

ssh -v machC ssh -v machC

You'll see it hop through those things. 您会看到它跳过那些事情。 But really you want a port forwarding and listener from machC to the ports on machD so you change the machC settings: 但是,实际上您希望从machC到machD上的端口进行端口转发和侦听,因此您可以更改machC设置:

host machC
     ProxyCommand ssh machB nc %h %p
     # first part is port on your current shell, second part is relative to machC
     LocalForward 1234 machD:portX
     RemoteForward 1235 localhost:portY

so using your example above: 因此,使用上面的示例:

host machineB
    hostname x.x.x.x
    user username_machineB

host machineC
    ProxyCommand ssh machineB nc %h %p
    hostname localhost
    user username_machineC
    localforward 1235 machineD:portX
    remoteforward 1236 localhost:portY

Then you can use command: 然后可以使用命令:

ssh machineC SSH机器C

use -v to see the hops and tunnels, and -N if you don't care about getting a shell. 使用-v查看跃点和隧道,如果您不关心获取shell,请使用-N。 Now you can talk to your localhost's port 1235 to send to machineD portX and read from 1236 to listen to machineC portY 现在,您可以与本地主机的端口1235进行通信,以发送至machineD portX,并从1236进行读取以侦听machineC portY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM