socat侦听分配给特定网络接口的所有IP [英] socat listen to all IPs assigned to a particular network interface

查看:100
本文介绍了socat侦听分配给特定网络接口的所有IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何让 socat 监听分配给一个特定网络接口的所有接口吗?我不确定是否有可能.

Does anyone know how do I get socat to listen on all interfaces assigned to one particular network interface ? I'm not sure if it's even possible.

如果我想将 socat 绑定到特定的 IP:PORT ,我总是从 ip 命令的输出中解析IP信息,然后像这样将 socat 绑定到它:

If I want to bind socat to a particular IP:PORT I always parse the IP info from the output of ip command and then bind socat to it like this:

NETIFC=$(ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1)
socat -d -d TCP4-LISTEN:1234,fork,bind=$(NETIFC) UNIX-CLIENT:/tmp/foo.sock

我认为我可以通过运行 socat 这样的方式来实现自己的目标:

I thought I would be able to achieve what I'm after by running the socat like this:

socat -d -d TCP4-LISTEN:1234,fork,so-bindtodevice = eth0 UNIX-CLIENT:/tmp/foo.sock

但是我相信我缺少一些底层的网络编程知识,因此不能完全理解此选项应该做的事情,只是明确希望它可以按照我的意愿去做,但是实际上实际上绑定到所有网络接口.

But I believe I'm missing some low level network programming knowledge and therefore don't quite get what is this option supposed to be doing and am just plainly hoping it should do what I want it to do, but in fact it actually binds to ALL network interfaces.

推荐答案

以下端口转发器在IP上侦听: xyzt:80 ,而在任何其他端口上均未绑定到 80 公开的IP(与bind参数不同).

The following port forwarder listens on IP: x.y.z.t:80, without binding to 80 on any other exposed IPs (unlike the bind parameter).

SOCAT_SOCKADDR=x.y.z.t socat TCP-LISTEN:80,reuseaddr,fork,su=nobody TCP:a.b.c.d:80

如果接口具有多个IP,则需要做一些尝试!在 ip v4,v6 的情况下,最好使用两个转发器.就我当前的目的而言,上面的代码就足够了.

If an interface has multiple IP’s, there is some experimenting to do! In the ip v4, v6 case it might be best to use two forwarders. For my current purposes, the code above is sufficient.

这篇关于socat侦听分配给特定网络接口的所有IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆