socat:通过 TTY 建立隧道 IP

发布于 2024-11-05 13:51:13 字数 799 浏览 1 评论 0原文

是否可以使用 socat 实用程序通过类似 ttyS0 的串行(调制解调器)设备获得双向 IP 隧道?我尝试使用 TUN 选项但仍然无法得到结果。

欢迎提出任何建议:)

更新:

PC1:

socat /dev/ttyUSB0,raw,echo=0,b57600,clocal TUN:192.168.1.1/24,up

PC2:

socat /dev/ttyUSB0,raw,echo=0,b57600,clocal TUN:192.168.1.2/24,up

之后,我看到 tun0 接口两端都有正确的地址,但我无法从另一个接口 ping 通。相反,当我使用 ping -c 1 192.168.1.1 发送数据时,远程 socat 进程退出,并且 tun0 设备被破坏。有什么建议吗?..

更新2:

当我们尝试仅使用 socat 通过串行建立 TCP/IP 隧道时,存在帧问题。 socat 的开发者 Gerhard Rieger 对我说:

恐怕你是对的。屯 通过数据报套接字工作,并且 - 通过 运气 - 也超过管道。但超过 串行线数据包边界可能 消失,这在发送时是致命的 tun接口上的数据输出。

我无法提供基于 socat 的解决方案 现在,抱歉。不过,我会尽力 稍后整合一些框架 版本 2 发布。

Is it possible to get an bidirectional IP-tunnel over ttyS0-like serial (modem) devices with the socat utility? I tried to use TUN option but still can't get the result.

Any suggestions are welcome :)

Update:

PC1:

socat /dev/ttyUSB0,raw,echo=0,b57600,clocal TUN:192.168.1.1/24,up

PC2:

socat /dev/ttyUSB0,raw,echo=0,b57600,clocal TUN:192.168.1.2/24,up

After that, I have seen tun0 interfaces with proper addresses on both ends but I can't ping one from other. Instead of that, when I send data with ping -c 1 192.168.1.1 remote socat process exits and it's tun0 device destroyed. Any suggestions?..

Update2:

There is a framing problem when we try to make TCP/IP tunnel over serial with only socat. Gerhard Rieger, the socat's developer, says me that:

I am afraid that you are right. tun
over datagram socket works, and - by
luck - also over pipes. But over the
serial line the packet boundaries may
vanish and this is fatal when sending
the data out on the tun interface.

I cannot offer a socat based solution
now, sorry. However, I will try to
integrate some framing in a later
version 2 release.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遥远的她 2024-11-12 13:51:13

哈哈,我可以工作,但需要一些魔法:)

所以,配置第一个对等点:

PC1:
1) slattach -L -s 57600 -p slip /dev/ttyUSB0 &
2) ifconfig sl0 up
3) socat TUN:192.168.1.1/24,up INTERFACE:sl0 &

...在第二个对等点上配置类似的东西:

PC2:
1) slattach -L -s 57600 -p slip /dev/ttyUSB0 &
2) ifconfig sl0 up
3) socat TUN:192.168.1.2/24,up INTERFACE:sl0 &

现在,你可以成功地从另一台电脑上 ping 通一台电脑:

PC1:
1) ping -c 5 192.168.1.2

PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.
64 bytes from 192.168.1.2: icmp_req=1 ttl=64 time=348 ms
64 bytes from 192.168.1.2: icmp_req=2 ttl=64 time=551 ms
64 bytes from 192.168.1.2: icmp_req=3 ttl=64 time=557 ms
64 bytes from 192.168.1.2: icmp_req=4 ttl=64 time=549 ms
64 bytes from 192.168.1.2: icmp_req=5 ttl=64 time=348 ms

--- 192.168.1.2 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4003ms
rtt min/avg/max/mdev = 348.116/471.143/557.128/100.177 ms

这有点由于使用了 slattach,所以有点棘手,但实际上任何其他解决方案都必须使用 slip 之类的东西来组织串行线路上的帧。例如,PPP 使用类似 HDLC 的帧。

Ha-ha, I works but there needs to be some magic :)

So, configure the 1st peer with:

PC1:
1) slattach -L -s 57600 -p slip /dev/ttyUSB0 &
2) ifconfig sl0 up
3) socat TUN:192.168.1.1/24,up INTERFACE:sl0 &

... and something like that on the 2nd peer:

PC2:
1) slattach -L -s 57600 -p slip /dev/ttyUSB0 &
2) ifconfig sl0 up
3) socat TUN:192.168.1.2/24,up INTERFACE:sl0 &

And now, you can successfully ping one PC from another:

PC1:
1) ping -c 5 192.168.1.2

PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.
64 bytes from 192.168.1.2: icmp_req=1 ttl=64 time=348 ms
64 bytes from 192.168.1.2: icmp_req=2 ttl=64 time=551 ms
64 bytes from 192.168.1.2: icmp_req=3 ttl=64 time=557 ms
64 bytes from 192.168.1.2: icmp_req=4 ttl=64 time=549 ms
64 bytes from 192.168.1.2: icmp_req=5 ttl=64 time=348 ms

--- 192.168.1.2 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4003ms
rtt min/avg/max/mdev = 348.116/471.143/557.128/100.177 ms

It's a little bit tricky because of slattach use but in fact any other solution must use something like slip to organize framing over the serial line. For example, PPP use HDLC-like frames.

千寻… 2024-11-12 13:51:13

根据我的尝试,您不需要 socat 来建立隧道。
您可以执行以下操作:

PC1:
 1, sudo slattach -s 19200 -p slip -dL /dev/ttyUSB0
 2, sudo ifconfig sl0 10.0.0.1/24 up
 3, sudo route add default gw 10.0.0.254 sl0

PC2:
 1, sudo slattach -s 19200 -p slip -dL /dev/ttyUSB0
 2, sudo ifconfig sl0 10.0.0.2/24 up
 3, sudo route add default gw 10.0.0.254 sl0

设置后,我可以从 PC1 ping PC2,反之亦然。

还有另一个前提条件:你的 Linux 内核必须加载 slip 模块。

based on what I have tried, you don't need socat to establish a tunnel.
you can just do the following:

PC1:
 1, sudo slattach -s 19200 -p slip -dL /dev/ttyUSB0
 2, sudo ifconfig sl0 10.0.0.1/24 up
 3, sudo route add default gw 10.0.0.254 sl0

PC2:
 1, sudo slattach -s 19200 -p slip -dL /dev/ttyUSB0
 2, sudo ifconfig sl0 10.0.0.2/24 up
 3, sudo route add default gw 10.0.0.254 sl0

After the setup, I can ping PC2 from PC1, and vice versa.

There is another pre-condition: your Linux kernel must have slip module loaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文