The socat utility is available as a binary package for the main *nix operating systems. However, since the current version, 1.6, was just released earlier this month, you might have to wait a while to get the latest and greatest, or download the source from the project's home page and install it with the usual
make, and (as root)
make install commands.
Beside socat itself, the package contains a set of files, scripts, and binaries that offer examples of what socat can do. Here's an introduction to some of its capabilities.
Even though there are more efficient methods to transmit files over a network, socat can come in handy on a freshly installed server or to process simple backups.
To transmit a file, you must run socat on the host that is receiving the file to listen for an incoming data stream. On the receiving host, run
socat TCP-LISTEN:4242filename. The TCP-LISTEN argument tells socat to listen on the port provided, and works with both IPv4 and IPv6. You can replace the port number, 4242 in this example, with any port you would like, but keep in mind that you need to run socat as root to bind a port below 1024.
To send the file, run
socat TCP:hostname:4242filename. This sends the file named "filename" to the host "hostname" through the port 4242. The hostname can be an IPv4 or IPv6 address or a fully qualified domain name (FQDN).
You can couple socat with tar to make a quick and simple backup. On the receiving host, run the same command as above. On the local server run
tar czf - /folder/to/backup | socat TCP:hostname:4242 -. The hyphen after the tar option tells tar to send its output to the standard output, which is then redirected thanks to the pipe to socat.
By default only fatal and error messages are displayed. You can increase socat's debug level with the
-d option, up to four times. The first level adds warning messages, the second adds info messages, and the fourth prints debug messages, such as library calls and their results. All messages can be written to stderr, a file, or to syslog.
-b<size> option sets the data transfer block size; data will be transmitted in blocks of size bytes. The
-T option can be use to set a timeout (in seconds) for when socat enters a transfer loop but nothing happens. Many others option are available; read the socat man page for additional information, or the README file located in the /usr/share/doc/ directory.
Instead of using filename in the sending command, you can use a hyphen to redirect the output to the standard out, which lets you interact with the remote host as a Telnet client does, but without some Telnet annoyances. For instance, Telnet isn't fit for use in a script and does not offer a listening mode.
Here is a way to use socat to get the date from an NTP server:
$ socat TCP:time.nist.gov:13 - 54178 07-03-19 10:42:02 50 0 0 427.6 UTC(NIST) *
Here is a way to list the most recent kernels:
socat TCP:kernel.org:79 -
Another interesting capability of socat is port forwarding, which allows you to route traffic from a local port to another port on a remote host.
To install a simple port forwarder, run the command
socat TCP-LISTEN:80,fork TCP:www.domain.org:80. The TCP-LISTEN argument tells socat to listen on local port 80 until a connection come in. Socat accepts it and forks it, then opens a connection on the remote host (www.domain.org in this example) and starts a data transfer. The
fork option allows more than a single connection by forking a new process after each accepted connection.
Socat can also forward UDP ports with the arguments UDP and UDP-LISTEN. The command becomes
socat UDP-LISTEN:80,fork UDP:www.domain.org:80.
You can build more complex port forwarders with socat, adding security (chroot, change process uid to the nobody user, etc.), command execution on the remote host (with output displayed on the client side), proxy authentication, and more.
Serial line emulation
socat READLINE,history=$HOME/.cmd_history /dev/ttyS0,raw,echo=0,crnl opens an interactive connection via the serial line (/dev/ttys0 in this example). The standard input is wrapped with the GNU readline command, which gives us the same editing power as a bash shell. The raw option tells socat to leave the standard in and standard out untouched. echo=0 disables local echo, while crnl convert newline characters for a correct display.
This feature can be used to maintain a dialog with serial devices with a command-line interface. Thus, if something goes wrong with one of your serial devices, socat lets you troubleshoot.
These demonstrations of socat's capabilities only scratch its surface. Socat is a powerful tool worth adding to your administrator's toolbox.
Julien Buty is a computer science student from the International Institute of Information Technology in Paris, France.