如何让 netcat 接受来自 LAN 外部的连接?

发布于 2024-08-26 21:13:46 字数 578 浏览 4 评论 0原文

我使用 netcat 作为后端来为我正在制作的程序来回传输数据。我在本地网络上测试了我的程序,一旦它工作,我认为只需从路由器转发端口即可让我的程序在互联网上工作。唉!事实似乎并非如此。

如果我使用以下命令在端口 6666 上启动 netcat 侦听:

nc -vv -l -p 6666 ,

然后在浏览器中转到 127.0.0.1:6666 ,正如预期的那样,我会看到HTTP GET 请求通过 netcat 发出(我的浏览器徒劳地等待)。但是,如果我访问 my.external.ip.address:6666,则根本没有任何结果,并且浏览器显示“无法连接到 my.external.ip.address:6666<” /代码>'。

我知道端口已正确转发,正如 www.canyouseeme.org 所说,端口 6666 已打开(当 netcat 未监听时,端口已关闭)。

如果我使用 -g my.adslmodem's.local.address 运行 netcat 来设置网关地址,我会得到相同的行为。我是否正确使用了这个命令行选项?对我做错了什么有任何见解吗?

I'm using netcat as a backend to shovel data back and forth for a program I'm making. I tested my program on the local network, and once it worked I thought it would be a matter of simply forwarding a port from my router to have my program work over the internet. Alas! This seems not to be the case.

If I start netcat listening on port 6666 with:

nc -vv -l -p 6666,

then go to 127.0.0.1:6666 in a browser, as expected I see a HTTP GET request come through netcat (and my browser sits waiting in vain). If I go to my.external.ip.address:6666, however, nothing comes through at all and the browser displays 'could not connect to my.external.ip.address:6666'.

I know that the port is correctly forwarded, as www.canyouseeme.org says port 6666 is open (and when netcat is not listening, that its closed).

If I run netcat with -g my.adslmodem's.local.address to set the gateway address, I get the same behavior. Am I using this command line option correctly? Any insight as to what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

平定天下 2024-09-02 21:13:46

您必须在公共接口上监听。您当前正在本地主机上侦听,计算机之外的任何人都无法连接。

您的情况可能会有所不同,但通过我的 netcat 实现,我有使用此命令行来侦听所有公共接口。

netcat -vv -l 0.0.0.0 6666

You must listen on the public interface. You're currently listening on localhost where no one outside of your computer can connect.

Your milage may vary, but with my implementation of netcat I have to use this command line to listen on all the public interfaces.

netcat -vv -l 0.0.0.0 6666

顾忌 2024-09-02 21:13:46

事实证明没有问题。之所以会出现这种情况,是因为我的路由器不允许发夹连接。也就是说,即使我已正确设置它,当源和目标都位于 NAT 之后时,路由器也不会建立连接。只要请求来自 LAN 外部,只需 ncat -l -p 6666 就可以正常工作。为了测试这一点,我用我的 3G 手机浏览到 my.external.ip.address:6666 ,果然,一个 HTTP 请求通过了:)

这个答案来自: 服务器故障问题,这是我应该首先问这个问题的地方。对此表示歉意。

Turns out there is no problem. It only appears that way because my router doesn't allow hairpin connections. That is, even though I've got it set up correctly, the router wouldn't make the connection when both source and destination are behind the NAT. Simply ncat -l -p 6666 works fine, so long as the request comes from outside the LAN. To test this I browsed to my.external.ip.address:6666 with my 3G mobile phone and sure enough, a HTTP request came through :)

This answer came from: a serverfault question, which is where I should have asked this question in the first place. Apologies for that.

朱染 2024-09-02 21:13:46

指定监听地址:

nc -l -s <LISTENING_IP_ADDR> -p <LISTENING_PORT>

在 Debian Jessie 上使用 netcat-traditional-1.10-41 进行测试

To specify a listening address:

nc -l -s <LISTENING_IP_ADDR> -p <LISTENING_PORT>

Tested on a Debian Jessie with netcat-traditional-1.10-41

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文