Linux command
socat 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Forward TCP port
socat TCP-LISTEN:[8080],fork TCP:[remote.host]:[80]
Create a simple TCP server
socat TCP-LISTEN:[1234],reuseaddr,fork EXEC:[/bin/cat]
Connect to a TCP port
socat - TCP:[host]:[port]
Forward Unix socket
socat TCP-LISTEN:[1234],fork UNIX-CONNECT:[/var/run/app.sock]
Create bidirectional pipe
socat EXEC:'[command1]' EXEC:'[command2]'
Create a simple chat server
socat TCP-LISTEN:[12345],fork,reuseaddr STDOUT
SSL/TLS connection
socat - OPENSSL:[host]:[443],verify=0
Serial port to TCP
socat TCP-LISTEN:[5000] /dev/ttyUSB0,b9600,raw,echo=0
Create virtual terminal pair
socat -d -d pty,raw,echo=0 pty,raw,echo=0
说明
socat (SOcket CAT) is a multipurpose relay tool that establishes two bidirectional byte streams and transfers data between them. It's like a more powerful netcat that supports numerous address types and protocols. Address types include TCP, UDP, Unix sockets, files, pipes, PTYs, processes (EXEC), SSL, serial devices, and many more. socat can connect, listen, and relay between any combination of these. Common use cases include port forwarding, protocol conversion, debugging network services, creating tunnels, and connecting disparate systems. It's invaluable for system administration and network troubleshooting.
参数
- TCP-LISTEN: _port_
- Listen on TCP port.
- TCP: _host_ : _port_
- Connect to TCP host:port.
- UDP: _host_ : _port_
- UDP connection.
- UNIX-CONNECT: _path_
- Connect to Unix socket.
- UNIX-LISTEN: _path_
- Listen on Unix socket.
- EXEC: _command_
- Execute command and connect to its I/O.
- OPENSSL: _host_ : _port_
- SSL/TLS connection.
- PTY
- Create pseudo-terminal.
- STDIO or -
- Standard input/output.
- FILE: _path_
- Open a file for reading and writing.
- PIPE: _path_
- Create or connect to a named pipe.
- SOCKS4: _host_ : _port_
- SOCKS4 proxy connection.
- fork
- Handle multiple connections (address option).
- reuseaddr
- Allow address reuse (address option).
- verify= _0|1_
- SSL certificate verification (address option).
- -d -d
- Verbose debugging output (repeat for more detail, up to -d -d -d -d).
- -v
- Verbose data transfer logging.
- -t _TIMEOUT_
- Total inactivity timeout in seconds.
- -T _TIMEOUT_
- Transfer timeout in seconds.
- -u
- Unidirectional mode (left to right only).
- -U
- Unidirectional mode (right to left only).
FAQ
What is the socat command used for?
socat (SOcket CAT) is a multipurpose relay tool that establishes two bidirectional byte streams and transfers data between them. It's like a more powerful netcat that supports numerous address types and protocols. Address types include TCP, UDP, Unix sockets, files, pipes, PTYs, processes (EXEC), SSL, serial devices, and many more. socat can connect, listen, and relay between any combination of these. Common use cases include port forwarding, protocol conversion, debugging network services, creating tunnels, and connecting disparate systems. It's invaluable for system administration and network troubleshooting.
How do I run a basic socat example?
Run `socat TCP-LISTEN:[8080],fork TCP:[remote.host]:[80]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does TCP-LISTEN: _port_ do in socat?
Listen on TCP port.