Socat (SOcket CAT) is a powerful command-line tool that establishes bidirectional data channels between various sources and destinations. It acts as a relay, enabling data to flow between processes, files, devices, and network sockets. Think of it as a multi-purpose adapter that connects disparate communication channels.
Core Uses of Socat
-
Networking and Tunneling:
-
Create TCP/UDP listeners and connect them to other hosts/ports.
-
Forward local ports to remote servers (port forwarding).
-
Establish encrypted tunnels (e.g., with OpenSSL) over insecure networks.
-
Create virtual serial ports over TCP/IP connections.
-
Debugging and Testing:
-
Capture and analyze network traffic between applications.
-
Simulate network conditions (latency, packet loss).
-
Inject test data into applications.
-
Inter-Process Communication (IPC):
-
Transfer data between unrelated processes using standard input/output.
-
Create named pipes (FIFOs) and communicate through them.
-
File Manipulation:
-
Read from or write to files, including over the network.
-
Concatenate and transform data streams.
Socat Command Structure
socat [OPTIONS] <ADDRESS1> <ADDRESS2>
OPTIONS: Control socat’s behavior (e.g., logging, timeouts, etc.).
-
ADDRESS1: The source address (e.g., TCP port, file, named pipe). -
ADDRESS2: The destination address (e.g., another TCP port, file). -
Simple TCP Relay:
socat TCP-LISTEN:8080,fork TCP:www.example.com:80
This listens on port 8080 and forwards connections to www.example.com[www.example.com](https://www.example.com) on port 80.
- Create a Virtual Serial Port:
socat -d -d PTY,link=/dev/ttyS10 TCP:192.168.1.100:2000
This creates a pseudo-terminal (/dev/ttyS10) that communicates over TCP.
- Debug Network Traffic:
socat -x -v TCP-LISTEN:8080,fork SYSTEM:"tcpdump -s 0 -w capture.pcap"
Captures network traffic on port 8080 and saves it to a capture.pcap file.
- Inter-Process Communication:
socat -u EXEC:"producer_app",stderr EXEC:"consumer_app"
Pipes the standard error output of producer_app to the standard input of consumer_app.
Flow Control
Socat lets you control the rate at which data flows between addresses, which is essential when connecting systems with different processing speeds or when simulating network conditions.
-u(Unidirectional): Forces data to flow only from the first address to the second.
socat -u TCP-LISTEN:9000,fork OPEN:/dev/null # Discard incoming data
-U(Unidirectional Reverse): Forces data to flow only from the second address to the first.
socat -U EXEC:"generate_data.sh" TCP:192.168.1.10:8888 # Send script output to a remote host
-b(Buffer Size): Sets the buffer size for data transmission.
socat -b 1024 TCP4-LISTEN:8080,fork TCP4:www.example.com:80 # 1KB buffer
2. Data Transformation
The system address type is a powerful tool for transforming data on the fly using external commands.
socat TCP-LISTEN:8080,fork SYSTEM:"sed 's/foo/bar/g'" # Replace 'foo' with 'bar' in incoming data
3. Logging
Socat provides detailed logging options to help troubleshoot connections and track data flow.
-
-d(Debug): Enable debugging output with varying levels (-d,-d -d, etc.). -
-lf: Log to a specified file. -
-v(Verbose): Increase the level of detail in log messages.
socat -v -lf socat.log TCP-LISTEN:8080,fork TCP:www.example.com:80
4. Timeouts
Set timeouts to gracefully handle connection failures and idle connections.
-
-T(Connection Timeout): Timeout for establishing a connection. -
-t(Activity Timeout): Timeout for inactivity on an established connection.
socat -T 10 -t 60 TCP-LISTEN:8080,fork TCP:www.example.com:80
5. Advanced Address Types
Socat supports a wide array of address types beyond just TCP and files:
-
OPENSSL: Establish encrypted connections. -
EXEC: Execute external commands. -
GOPEN: Open files in read/write mode. -
SCTP: Stream Control Transmission Protocol. -
UNIX: Unix domain sockets. -
…and many more: Refer to the
socatmanual for a complete list.
Example: OpenSSL Encryption
socat OPENSSL-LISTEN:4433,cert=server.crt,key=server.key,verify=0,fork TCP:localhost:80
This creates a secure, encrypted tunnel to localhost on port 80, using the specified certificate and key.
Tips and Gotchas
-
Security: Be cautious with port forwarding and tunneling. Secure your connections (e.g., with SSH tunneling or SSL).
-
Resource Management: The
forkoption creates a new process for each connection. Use it judiciously to avoid resource exhaustion. -
Debugging: The
-d(debug) and-x(hexdump) options are invaluable for troubleshooting. -
Address Types: Socat supports a vast array of address types. Refer to the manual for details.