cat

Getting started with socat, a multipurpose relay tool for Linux – Red Hat

the socat utility is a relay for bi-directional data transfers between two independent data channels.

there are many different types of channels that socat can connect to, including:

  • files
  • tubes
  • devices (serial line, pseudo-terminal, etc.)
  • sockets (unix, ip4, ip6 – raw, udp, tcp)
  • ssl sockets
  • proxy connection connections
  • file descriptors (stdin, etc.)
  • the gnu line editor (readline)
  • programs
  • combinations of two of these
  • This tool is considered the advanced version of netcat. they do similar things, but socat has more additional features, such as allowing 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 some examples:

    • tcp port forwarder (one-shot or daemon)
    • outer socks
    • tool to attack weak firewalls (security and auditing)
    • shell interface for unix sockets
    • ip6 relay
    • redirect tcp-oriented programs to a serial line
    • logically connect serial lines on different computers
    • set up a relatively safe environment (su and chroot) to run client or server shell scripts with network connections
    • [download now: an automation guide for system administrators. ]

      how do we use socat?

      the socat syntax is quite simple:

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

      socat usage examples

      Let’s start with some basic examples of using socat for various connections.

      1. connect to tcp port 80 on the local or remote system:

      in this case, socat transfers data between stdio (-) and a tcp4 connection to port 80 on a host named www.example.com.

      2. use socat as a tcp port forwarder:

      for a single connection, enter:

      for multiple connections, use the branch option as used in the examples below:

      This example listens on port 81, accepts connections, and forwards the connections to port 80 on the remote host.

      The above example listens on port 3307, accepts connections, and forwards the connections to a unix socket on the remote host.

      [ learn how to manage your linux environment for success. ]

      3. implement a simple network-based message collector:

      In this example, when a client connects to port 3334, a new child process is spawned. all data sent by clients is appended to the /tmp/test.log file. if the file does not exist, socat creates it. the reuseaddr option allows an immediate restart of the server process.

      4. send a broadcast to the local network:

      in this case, socat transfers data from stdin to the specified multicast address using udp over port 6666 for local and remote connections. the command also tells the eth0 interface to accept multicast packets for the given group.

      practical uses of socat

      socat is a great troubleshooting tool. it is also useful for easy remote connections. pretty much, i have used socat for remote mysql connections. In the following example I demonstrate how I use socat to connect my web application to a remote mysql server by connecting via the local socket.

      1. on my remote mysql server, i enter:

      this command starts socat and configures it to listen using port 3307.

      2. on my web server, I enter:

      the above command connects to the remote server 192.168.100.5 using port 3307.

      however, all communication will be done on the unix socket /var/lib/mysql/mysql.sock, and this makes it look like a local server.

      summarize

      socat is a sophisticated utility and indeed an excellent tool for all system administrators to get things done and troubleshoot. follow this link to read more examples of using socat.

      [ Free Online Course: Red Hat Enterprise Linux Technical Overview. ]

Related Articles

Back to top button