socat的介绍与使用

什么是socat?

socat 是一个实用的命令行工具。


常用方法

端口转发

# 端口转发
#socat -d -d -lf /var/log/socat.log TCP4-LISTEN:6666,bind=127.0.0.1,reuseaddr,fork TCP:127.0.0.1:3306
#mysql -uroot -h127.0.0.1 -P6666 -p

文件写入

#不使用group则默认当前用户
socat -T 2 -u - CREATE:./james,group=test,perm=0640 
socat -t 2 -u - CREATE:./james,perm=0640

读取文件

socat - /tmp/jamestest
socat - GOPEN:./james
socat - OPEN:./james

socat调用命令

socat - EXEC:ls


后续补充

详细操作参数介绍

socat [options] <bi-address> <bi-address>

options

  • -V print version and feature information to stdout, and exit

将版本和功能信息打印到stdout,然后退出

  • -h|-? print a help text describing command line options and addresses

打印帮助信息

  • -hh like -h, plus a list of all common address option names

打印比-h更详细的帮助信息

  • -hhh like -hh, plus a list of all available address option names

打印最详细的帮助信息

  • -d increase verbosity (use up to 4 times; 2 are recommended)

多个使用-d 打印fatal, error, warning, notice, info, and debug messages日志级别(建议使用两个-d,最多使用4个-d)

  • 通过-d -d 显示调试信息
  • -D analyze file descriptors before loop

在开始传输阶段之前记录有关文件描述符的信息

  • -ly[facility] log to syslog, using facility (default is daemon)

将消息写入syslog而不是stderr; 使用-d选项定义的严重性。 使用可选的<facility>,可以选择syslog类型,默认为“daemon”。 第三方库可能不遵守此选项

auth, authpriv:与认证有关的系统,例如 login, su 等需要user names and passwords的服务
uucp: UUCP系统
daemon:与各个 daemon 有关的系统
kern :核心(kernel)系统
lpr:列印相关的系统
mail:与邮件相关的系统
news:与新闻群组相关的系统
syslog:与 syslogd本身相关的系统
user:一般使用者的系统
local0 ~ local7:保留, 做為特別设定使用的

参考文件

  • -lf<logfile> log to file

将消息写入<logfile> [filename]而不是stderr。 某些第三方库,特别是libwrap,可能不遵守此选

  • -ls log to stderr (default if no other log)

将消息写入stderr(这是默认值)。 某些第三方库可能不遵守此选项,特别是libwrap似乎只记录到syslog。屏幕输出

  • -lm[facility] mixed log mode (stderr during initialization, then syslog)

混合日志输出模式

  • -lp<progname> set the program name used for logging

覆盖错误消息中打印的程序名称,用于构造环境变量名称。

  • -lu use microseconds for logging timestamps

使用微秒记录时间戳

  • -lh add hostname to log messages

增加hostname到日志文件中

  • -v verbose data traffic, text

详细的数据流量,文本

  • -x verbose data traffic, hexadecimal

详细数据流量,十六进制

  • -b<size_t> set data buffer size (8192)

设置数据缓冲区大小(8192)

  • -s sloppy (continue on error)

遇到错误继续不退出

  • -t<timeout> wait seconds before closing second channel

关闭第二个频道前等待几秒钟

#不活跃不退出,有下一个输入下一个指令超过两秒则退出。
socat -t 2  - TCP:127.0.0.1:80 
  • -T<timeout> total inactivity timeout in seconds

不活动超时(以秒为单位),适用于UTP协议

#2s 不活跃两秒后退出
socat -T 2 - TCP:127.0.0.1:80
  • -u unidirectional mode (left to right)

单向模式(从左到右)

  • -U unidirectional mode (right to left)

单向模式(从右到左)

  • -g do not check option groups

不要检查选项组

  • -L <lockfile> try to obtain lock, or fail

如果存在lockfile,则退出并显示错误。 如果lockfile不存在,则创建它并继续,在退出时取消链接lockfile

  • -W <lockfile> try to obtain lock, or wait

如果存在lockfile,则等待直到它消失。 当lockfile不存在时,创建它并继续,在退出时取消链接lockfile。

  • -4 prefer IPv4 if version is not explicitly specified

如果没有明确指定版本,则使用ipv4

  • -6 prefer IPv6 if version is not explicitly specified

使用ipv6


参考文件

man pages

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容