bash Mac OS X 10.8.4 上的 nc (netcat) 卡住

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18578565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:26:10  来源:igfitidea点击:

nc (netcat) on Mac OS X 10.8.4 gets stuck

bashnetworkingosx-mountain-lionsshdnetcat

提问by ZombieNinjaPirate

I encountered a little issue while using the nc utility on Mac OS X, a utility i often use as a quick and dirty solution to check if a port is open and what version the daemon is running.

我在 Mac OS X 上使用 nc 实用程序时遇到了一个小问题,我经常使用该实用程序作为快速而肮脏的解决方案来检查端口是否打开以及守护程序正在运行的版本。

We deployed a new set of computers the other day and i wanted to verify what version of sshd they were running, without having to leave my chair.

前几天我们部署了一组新计算机,我想验证它们运行的​​是哪个版本的 sshd,而不必离开我的椅子。

This is the command i ran and the resulting output:

这是我运行的命令和结果输出:

$ for i in {183..200}; do echo "hello" | nc -n -w 2 -v 10.120.113.$i 22; done
Connection to 10.120.113.183 22 port [tcp/*] succeeded!
SSH-2.0-OpenSSH_5.9
Protocol mismatch.
nc: connect to 10.120.113.184 port 22 (tcp) failed: Connection refused
^C
$

It finds the first machine on 183 and returns the daemon version, it dont look like the sshd is running on 184, but when it hits 185 it simply stops and i have to kill it with ctrl+c.

它在 183 上找到第一台机器并返回守护程序版本,它看起来不像 sshd 在 184 上运行,但是当它达到 185 时它只是停止,我必须用 ctrl+c 杀死它。

As i have understood the man page for nc it should time out when using the '-w' switch, but it don't. Its the same issue from multiple machines.

正如我所了解的 nc 手册页,它应该在使用“-w”开关时超时,但它没有。它来自多台机器的相同问题。

Is this simply the case of me misunderstanding the man page? Is there any other way to make nc time out after X seconds if don′t receive any response? Is there any other way to do this using the builtin tools in Mac OS X?

这仅仅是我误解手册页的情况吗?如果没有收到任何响应,还有其他方法可以让 nc 在 X 秒后超时吗?有没有其他方法可以使用 Mac OS X 中的内置工具来做到这一点?

I′ve also tried running nc with only the '-z' switch with the same results. The machines are placed in our production so i′m not allowed to install any 3rd party applications like nmap.

我也试过只用“-z”开关运行 nc ,结果相同。这些机器被放置在我们的生产中,所以我不允许安装任何 3rd 方应用程序,如 nmap。

Platform: Mac OS X 10.8.4
Executable: /usr/bin/nc

Sorry if this question has been answered, i searched but could not find any solution to this.

抱歉,如果此问题已得到解答,我进行了搜索,但找不到任何解决方案。

回答by Kevin Burdett

I believe you are looking for the -Goption. From the man pages:

我相信你正在寻找这个-G选项。从手册页:

-G conntimeout TCP connection timeout in seconds.

-G conntimeout TCP 连接超时(以秒为单位)。

-wis used to set the timeout afterconnection. -Goption is used to set the timeout beforeconnection. This should give you what you want

-w用于设置连接的超时时间。-G选项用于设置连接的超时时间。这应该给你你想要的

nc -n -G 2 -v xxx.xxx.xxx.xxx 22

回答by Beartech

I tried this on my Mac running 10.8.4 and after about 6 minutes it was looking like:

我在运行 10.8.4 的 Mac 上尝试了这个,大约 6 分钟后它看起来像:

andys-MacBook-Pro:EquipDB uw$ for i in {183..200}; do echo "hello" | nc -n -w 2 -v 10.120.113.$i 22; done
nc: connect to 10.120.113.183 port 22 (tcp) failed: Operation timed out
nc: connect to 10.120.113.184 port 22 (tcp) failed: Operation timed out
nc: connect to 10.120.113.185 port 22 (tcp) failed: Operation timed out
nc: connect to 10.120.113.185 port 22 (tcp) failed: Operation timed out

So it is timing out on mine, but just takes quite a while.

所以我的超时了,但只需要很长时间。

Hmmm... was thinking my test would be pointless because I will never actually connect to something since I'm not on your network. But I did see this:

嗯...我以为我的测试毫无意义,因为我永远不会真正连接到某些东西,因为我不在你的网络上。但我确实看到了这个:

-w # => Timeout after # seconds

Note that -w also sets the network inactivity timeout. This does not have any effect until standard input closes, but then if nothing further arrives from the network in the next seconds, netcat tries to read the net once more for good measure, and then closes and exits. There are a lot of network services now that accept a small amount of input and return a large amount of output, such as Gopher and Web servers, which is the main reason netcat was written to ``block'' on the network staying open rather than standard input. Handling the timeout this way gives uniform behavior with network servers that don'tclose by themselves until told to.

This works for final net reads but not for connections.

-w # => # 秒后超时

请注意, -w 还设置网络不活动超时。这在标准输入关闭之前没有任何影响,但是如果在接下来的几秒钟内没有来自网络的进一步到达,netcat 会尝试再次读取网络以进行良好测量,然后关闭并退出。现在有很多网络服务接受少量输入并返回大量输出,例如Gopher和Web服务器,这是netcat在网络上被写入“阻塞”而不是保持开放的主要原因比标准输入。以这种方式处理超时为网络服务器提供了统一的行为,这些服务器在被告知之前不会自行关闭。

这适用于最终的网络读取,但不适用于连接。

If I'm understanding this right it only times out once it connects, since mine never connects it just has a built in timeout that has nothing to do with -w? found here, helpful?

如果我正确理解这一点,它只会在连接后超时,因为我的从未连接过它只是有一个与 -w 无关的内置超时?在这里找到,有帮助吗?