
linux socat
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.
从系统角度来看,Linux提供了不同的原理和用例。 socat是一个非常有趣的命令,它使我们能够从不同类型的系统资源(例如网络,文件,命令,套接字等)重定向输入和输出。
为Debian,Ubuntu,Mint和Kali安装 (Install For Debian, Ubuntu, Mint and Kali)
socat
can be install dpkg based distributions like below by using apt
command.
可以使用apt
命令像下面那样安装socat
基于dpkg的发行版。
$ sudo apt install socat -y

为Fedora,CentOS,RHEL安装(Install For Fedora, CentOS, RHEL)
We can install for Fedora, CentOS and RHEL like below.
我们可以像下面那样为Fedora,CentOS和RHEL安装。
$ sudo yum install socat
句法 (Syntax)
Syntax of socat is very simple. We need to just provide the input and output with related options.
socat的语法非常简单。 我们只需要为输入和输出提供相关选项即可。
socat INPUT_TYPE(OPTIONS) OUTPUT_TYPE(OPTIONS)
- INPUT_TYPE specifies the file, stream or network socket we will read INPUT_TYPE指定我们将读取的文件,流或网络套接字
- OUTPUT_TYPE specifies file, stream or network socket we will writeOUTPUT_TYPE指定我们将编写的文件,流或网络套接字
- OPTIONS specifies some configuration about INPUT or OUTPUTOPTIONS指定有关INPUT或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
键入引用文件,命令,流或网络。 INPUT和OUTPUT可以是不同的类型。 例如,如果要使用系统命令作为类型,则需要将SYSTEM
指定为INPUT_TYPE。 类型清单
- SYSTEM系统
- TCPTCP协议
- UDPUDP协议
- GOPEN开普
- STDIO or –STDIO或–
- PTY 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.
选项用于提供有关INPUT类型的参数和详细信息。 在INPUT之后通过用:
分隔来添加选项。 例如,为了指定PIPE路径,我们将使用以下行。
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.
我们将从一个简单的例子开始。 我们将使用SYSTEM
运行系统命令并提供命令ls
然后将输出写入标准输出。
$ 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.
我们也可以从标准输入中读取内容。 在此示例中,我们将从标准输入中读取内容并将其写入名为test
的文件中。 我们将使用STDIO
来指定标准输入。
$ 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.
如我们所见,我们提供了文件名作为选项和create
语句,如果不存在,它将创建一个新文件。
打开网络套接字和监听端口(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
。我们将打开TCP和IP版本4端口以监听端口号1234。这将是创建新进程的分支进程。 输出将是一个名为capture
的文件,并将新到达的数据附加到文件末尾。
$ 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.
因为我们已经准备好使用了,但我想表达append
。 append
选项将添加到给定文件的末尾,如下所示。
$ 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.
这似乎有些怪异,但是由于我们可以使用不同类型的协议和套接字。 我们可以连接一个网页并检索打印到标准输出的网页。 在此示例中,我们将连接到poftut.com
TCP端口80并打印到标准输出。 我们将提供GET /
从标准输入到获取页面。
$ socat - TCP:poftut.com:www,crnl

翻译自: https://www.poftut.com/linux-multipurpose-relay-socat-command-tutorial-with-examples/
linux socat