socat: tunnel IP through TTY

北战南征 提交于 2019-11-28 17:40:54

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.

user0814

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!