Using Socat and Netcat
socat stands for SOCket CAT, it is considered the advanced version of netcat. Both of these tools are useful for setting up and testing bidirectional data transfers over independent channels
Types of channels that can be used with socat
- Files
- Pipes
- Devices (serial line, pesudo-terminal)
- Sockets (UNIX, IP4, UDP, TCP)
- SSL sockets
- File descriptors (Stdin)
Syntax
netcat
sh
netcat [options] <host> <port>
netcat [options] <host> <port>
socat
sh
socat [options] <address> <address>
socat [options] <address> <address>
where each <address>
follows the syntax protocol:ip:port
.
Examples
Set up a dummy client-server on localhost
On one terminal, start a TCP listener on port 4444, forwarding the data to Stdout
sh
nc -l 4444
# or
socat TCP4-LISTEN:4444 STDOUT
nc -l 4444
# or
socat TCP4-LISTEN:4444 STDOUT
On another terminal, forward data from Stdin to port 4444 over TCP on localhost
sh
nc localhost 4444
# or
socat STDIN TCP:localhost:4444
nc localhost 4444
# or
socat STDIN TCP:localhost:4444
On the second terminal, type anything you like and see it appear on the other terminal.
Forward a UDP port to a serial device
socat UDP-L:4445 /dev/ttyUSB0
socat UDP-L:4445 /dev/ttyUSB0
All of the data arriving at UDP port 4445 will be forwarded to the serial device /dev/ttyUSB0
Tips
- socat allows the shortcut address
-
to be used in place of Stdin/Stdout (e.g.socat TCP4-LISTEN:4444 -
)
ADDITIONAL RESOURCES
- https://www.redhat.com/sysadmin/getting-started-socat
- https://copyconstruct.medium.com/socat-29453e9fc8a6
- http://www.dest-unreach.org/socat/doc/socat.html#EXAMPLES
- https://www.digitalocean.com/community/tutorials/how-to-use-netcat-to-establish-and-test-tcp-and-udp-connections
- https://jameshfisher.com/2018/03/04/create-udp-connection-with-netcat/