linux socat_Linux多用途中继Socat命令教程和示例

socat是Linux上一个强大的工具,能重定向输入和输出到不同资源,如网络、文件、命令和套接字。本文介绍了在Debian、Ubuntu等系统上安装socat的方法,其简单的语法、类型选择(如TCP、UDP)以及各种使用场景,如写入标准输出、从标准输入读取、打开网络套接字监听、追加到文件,甚至通过标准输出浏览网页。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

linux socat

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
Install For Debian, Ubuntu, Mint and Kali
Install For Debian, Ubuntu, Mint and Kali
为Debian,Ubuntu,Mint和Kali安装

为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 write

    OUTPUT_TYPE指定我们将编写的文件,流或网络套接字
  • OPTIONS specifies some configuration about INPUT or OUTPUT

    OPTIONS指定有关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

    系统
  • TCP

    TCP协议
  • UDP

    UDP协议
  • 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 -
Write To Standard Output
Write To Standard Output
写入标准输出

从标准输入读取(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语句,如果不存在,它将创建一个新文件。

LEARN MORE  Linux /bin/bash Shell and Script Tutorial
了解更多Linux / bin / bash Shell和脚本教程

打开网络套接字和监听端口(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.

因为我们已经准备好使用了,但我想表达appendappend选项将添加到给定文件的末尾,如下所示。

$ 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
Surf In The Web Over Standard Output
Surf In The Web Over Standard Output
通过标准输出上网

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

linux socat

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值