最後更新: 2018-09-21
介紹
socat (SOcket CAT) 是一個可以用來做 bidirectional data transfer 的工具來.
在 relay 方面, 它比 nc 強大.
用法:
# 查看 Version 及 Features
socat -V
# 一般 Usage
socat [options] <address1> <address2>
功用:
address1 ---> address2
options:
-lf <logfile> # Writes messages to <logfile> [filename] instead of stderr.
-L<lockfile> # If lockfile exists, exits with error.
W<lockfile> # If lockfile exists, waits until it disappears.
-u # Uses unidirectional mode. address1: reading, address2: writing
Address 的格式:
address type separated by ":"
address options separated by ','
Type:
- TCP-LISTEN:<port>
- OPEN:<filename>
- TCP:<host>:<port>
- EXEC:<command-line>
Example
# listen port 12345
socat tcp-listen:12345 -
# Send "test" to remote(192.168.123.10)
echo "test" | socat - tcp:192.168.123.10:12345
# Send file
# 收
ip1 # socat -u open:a.txt tcp-listen:9000
# 發
ip2 # socat -u tcp:ip1:9000 open:a.txt,create
# Serail port to network
socat /tmp/serial tcp-listen:9000 &
# Redirect
socat TCP4-LISTEN:80,reuseaddr,fork TCP4:192.168.123.12:8080
多連接 時要用到的 options
fork:
After establishing a connection, handles its channel in a child process and
keeps the parent process attempting to produce more connections,
either by listening or by connecting in a loop
reuseaddr:
Allows other sockets to bind to an address even if parts of it (e.g. the local port) are already in use by socat (example).
# Run Prage when connect
socat TCP-LISTEN:1234,reuseaddr,fork EXEC:./helloworld
Unix Socket
# OpenVPN STATISTICS
echo status | socat - unix-connect:/var/lib/openvpn/mgt-nas.sock
# As telnet
socat - unix-connect:/var/lib/openvpn/mgt-nas.sock