If you need an advanced sysadmin tool for your toolbox, this is the one. Socat allows for bidirectional data transfers from one location to another.

The socat utility is a relay for bidirectional data transfers between two independent data channels. There are many different types of channels socat can connect, including:

  • Files
  • Pipes
  • Devices (serial line, pseudo-terminal, etc)
  • Sockets (UNIX, IP4, IP6 – raw, UDP, TCP)
  • SSL sockets
  • Proxy CONNECT connections
  • File descriptors (stdin, etc)
  • The GNU line editor (readline)
  • Programs
  • Combinations of two of these

This tool is regarded as the advanced version of netcat. They do similar things, but socat has more additional functionality, such as permitting multiple clients to listen on a port, or reusing connections.

Why do we need socat?

There are many ways to use socate effectively. Here are a few examples:

  • TCP port forwarder (one-shot or daemon)
  • External socksifier
  • Tool to attack weak firewalls (security and audit)
  • Shell interface to Unix sockets
  • IP6 relay
  • Redirect TCP-oriented programs to a serial line
  • Logically connect serial lines on different computers
  • Establish a relatively secure environment (su and chroot) for running client or server shell scripts with network connections

How do we use socat?

The syntax for socat is fairly simple:

socat [options] <address> <address>

You must provide the source and destination addresses for it to work. The syntax for these addresses is:

protocol:ip:port

Practical uses for socat

Socat is a great tool for troubleshooting. It is also handy for easily making remote connections. Practically, I have used socat for remote MySQL connections. In the example below, I demonstrate how I use socat to connect my web application to a remote MySQL server by connecting over the local socket.

1. On my remote MySQL server, I enter:

# socat TCP-LISTEN:3307,reuseaddr,fork UNIX-CONNECT:/var/lib/mysql/mysql.sock &

2. On my webserver, I enter:

# socat UNIX-LISTEN:/var/lib/mysql/mysql.sock,fork,reuseaddr,unlink-early,user=mysql,group=mysql,mode=777 TCP:192.168.100.5:3307 &

However, all communication will be done on the Unix socket /var/lib/mysql/mysql.sock, and this makes it appear to be a local server.

Wrap up

socat is a sophisticated utility and indeed an excellent tool for every sysadmin to get things done and for troubleshooting.

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like