Linux provides different philosophy and use cases from system point of view. `socat` is very interesting command which provides us the ability to redirect input and outputs from different type of system resources like network, file, command, socket etc.
Install For Debian, Ubuntu, Mint and Kali
socat
can be install dpkg based distributions like below by using apt
command.
$ sudo apt install socat -y

Install For Fedora, CentOS, RHEL
We can install for Fedora, CentOS and RHEL like below.
$ sudo yum install socat
Syntax
Syntax of socat is very simple. We need to just provide the input and output with related options.
socat INPUT_TYPE(OPTIONS) OUTPUT_TYPE(OPTIONS)
- INPUT_TYPE specifies the file, stream or network socket we will read
- OUTPUT_TYPE specifies file, stream or network socket we will write
- OPTIONS specifies some configuration about INPUT or OUTPUT
Types
Type refer to the file, command, stream or network. INPUT and OUTPUT can be different types. For example if we want to use a system command as type we need to specify SYSTEM
as INPUT_TYPE . List of types
- SYSTEM
- TCP
- UDP
- GOPEN
- STDIO or –
- PTY
- PIPE
- …
OPTIONS
Options are used to provide parameters and details about INPUT types. OPTIONS are added after INPUT by separating with :
. for example in order to specify a PIPE path we will use following line.
PIPE:/tmp/test/pof
Write To Standard Output
We will start with simple example. We will run a system command with SYSTEM
and providing command ls
and than write output to the standard output.
$ socat SYSTEM:ls -

Read From Standard Input
We can also read from standard input too. In this example we will read from standard input and write into a file named test
. We will use STDIO
to specify standard input.
$ socat STDIO FILE:/home/ismail/test,create
As we can see we have provided the file name as option and the create
statement which will create a new file if it is not exist.
Open Network Socket and Listen Port
We can also use network sockets with socat
.We will open TCP and IP version 4 port for listening port number 1234. This will be forked process where new process will be created. The output will be a file named capture
and append newly arrived data to the end of file.
$ socat TCP4-LISTEN:1234,reuseaddr,fork gopen:/home/ismail/capture,seek-end=0,append
Append To A File
As we have all ready used but I want to express the append
. append
option will add to the end of the given file like below.
$ socat FILE:/tmp/test1 FILE:/tmp/test:append
Surf In The Web Over Standard Output
This may be seem a bit weird but as we can use different type of protocols and sockets. We can connect a web page and retrieve web pages printing to the standard output. In this example we will connect to the poftut.com
TCP port 80 and print to the standard output. We will provide GET /
from standard input to get pages.
$ socat - TCP:poftut.com:www,crnl
