【Linux】命令 - socat

Posted by 西维蜀黍 on 2021-07-17, Last Modified on 2022-12-10

socat

socat - Multipurpose relay (SOcket CAT)

Socat is a command line based utility that establishes two bidirectional byte streams and transfers data between them. Because the streams can be constructed from a large set of different types of data sinks and sources (see address types), and because lots of address options may be applied to the streams, socat can be used for many different purposes.

Parameter

  • fork: After establishing a connection, handles its channel in a child process and keeps the parent process attempting to produce more connections, either by listening or by connecting in a loop.

包含 fork 和不包含的区别:

  • -d: Without this option, only fatal and error messages are generated; applying this option also prints warning messages. See DIAGNOSTICS for more information.
  • -d -d: Prints fatal, error, warning, and notice messages.
  • -d -d -d: Prints fatal, error, warning, notice, and info messages.
  • -d -d -d -d: Prints fatal, error, warning, notice, info, and debug messages.

Example

TCP Clients

Transfers data between STDIO (-) and a TCP4 connection to port 80 of host www.domain.org.

$ socat - TCP4:www.domain.org:80

TCP Communication via socat

Create a simple client/server TCP connection

Server

# Listen to a port, wait for an incoming connection and transfer data to STDIO:
$ socat TCP-LISTEN:8800\
,reuseaddr\
,pf=ip4\
,fork -

Client

# Create a connection to a host and port, transfer data in STDIO to connected host:
$ socat TCP:localhost:8800 -

这样就可以在terminal 通过TCP进行通信了:

TCP Jump Server/ Forwarder

# Forward incoming data of a local port to another host and port:
$ socat TCP-LISTEN:80,fork TCP4:www.example.com:80
# A simple TCP port forwarder
$ socat TCP4-LISTEN:www TCP4:www.domain.org:www

# similarly
$ socat TCP4-LISTEN:8882,fork TCP4:localhost:8800

Unix Domain Socket

single (bi-directional) endpoint

Server

$ socat UNIX-LISTEN:/tmp/a.sock -
sdsd

Client

$ socat UNIX-LISTEN:/tmp/a.sock -
sdsd

mutliple (bi-directional) endpoints

Server

$ socat UNIX-LISTEN:/usr/local/var/run/test/test.sock\
,fork -

Client

$ socat UNIX-CONNECT:/usr/local/var/run/test/test.sock -

Practical uses for socat

Proxy for MySQL

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.

On my remote MySQL server, I enter:

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

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

. 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 &

The above command connects to the remote server 192.168.100.5 by using port 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.

Command execution

Execute shell commands on a remote server (i.e. basic ssh client).

Server

$ socat TCP-LISTEN:1234 EXEC:/bin/bash

Client

$ socat TCP:localhost:1234 -
pwd
/Users/shiwei

Tunneling

Create an encrypted tunnel between a local computer and a remote machine to relay services created through an SSH protocol connection.

Server

$ socat TCP-LISTEN:54321\
,reuseaddr\
,fork \
TCP:remote.server.com:22

Client

$ ssh root@localhost -p 54321

Create a virtual point-to-point IP link through a TUN network device.

Reference