TCP/UDP port forwarding with socat
TCP port forwarding
socat -d TCP4-LISTEN:80,reuseaddr,fork TCP4:127.0.0.1:8080
# specify listening address
socat -d TCP4-LISTEN:80,reuseaddr,fork,bind=127.0.0.1 TCP4:10.10.1.1:8888
UDP port forwarding
socat -T 600 UDP4-LISTEN:5353,reuseaddr,fork UDP4:114.114.114.114:53
File transfer
Server side
socat -u open:FILENAME tcp-listen:12345
Client side
socat -u tcp:ServerIP:12345 open:LOCALFILE,create
-u
表示数据单向传送,从第一个参数传递到第二个参数-U
则表示从第二个参数传送到第一个参数open
表示使用系统调用open()
打开文件,不能打开unix
域socket
tcp-listen
表示监听tcp
端口create
表示如果文件不存在则创建- 传输结束后两端均退出
Split Read/Write
- 使用
!!
符号,左侧表示读,右侧表示写
socat open:hello.html\!\!open:log.txt,create,append tcp-listen:12345,reuseaddr,fork
open:hello.html
表示读hello.html
文件open:log.txt
表示收到的数据写入log.txt
文件reuseaddr
见socket
的SO_REUSEADDR
fork
请求到达时,fork
一个进程进行处理- 在
bash下
,需要用\
对!
进行转义
Reference
https://man.imzye.me/Linux/socat/
http://www.dest-unreach.org/socat/doc/socat.html
Back to Table of Contents
Disclaimer
- License under
CC BY-NC 4.0
- Copyright issue feedback
me#imzye.com
, replace # with @ - Not all the commands and scripts are tested in production environment, use at your own risk
- No personal information is collected.
Feedback