bash netcat 超时问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24198456/
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-10 00:52:48  来源:igfitidea点击:

Issue with netcat timeout

bashnetcat

提问by anditpainsme

Why does the following netcat command not time out if the attempt to connect takes longer than 3 seconds (ie: when the port isn't open)? I assumed that the -w flag would be what I needed. OS is OSX 10.9.

如果尝试连接的时间超过 3 秒(即:端口未打开时),为什么以下 netcat 命令不会超时?我认为 -w 标志将是我所需要的。操作系统是 OSX 10.9。

nc -v -z -w 3 127.0.0.1 5050

nc -v -z -w 3 127.0.0.1 5050

Assuming that worked, I planned to implement like this (unsure if this will work, total bash noob)

假设有效,我计划像这样实施(不确定这是否有效,完全是 bash noob)

nc -v -z -w 3 127.0.0.1 5050 | /dev/null && echo "Online" || echo "Offline"

nc -v -z -w 3 127.0.0.1 5050 | /dev/null && echo "Online" || echo "Offline"

回答by CDahn

You need to redirect to /dev/null, not pipe to it. Try the following:

您需要重定向到 /dev/null,而不是通过管道连接到它。请尝试以下操作:

nc -v -z -w 3 127.0.0.1 5050 &> /dev/null && echo "Online" || echo "Offline"

On my machine, port 5050 isn't open, and I get the following:

在我的机器上,端口 5050 未打开,我得到以下信息:

$ nc -v -z -w 3 localhost 5050 &> /dev/null && echo "Online" || echo "Offline"
Offline

回答by Thiago Ruiz

There is an old bug report about this on debian (https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=97583) and still having the same behavior in Debian GNU/Linux 7.7 (wheezy)

在 debian ( https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=97583)上有一个关于此的旧错误报告,并且在 Debian GNU/Linux 7.7 中仍然具有相同的行为 (wheezy)

I found a "solution" to this: installing the openbsd version of nc:

我找到了一个“解决方案”:安装 nc 的 openbsd 版本:

apt-get install netcat-openbsd

回答by Mike Q

Nc:nc is usually installed already , however on some systems such as Mac OS X, the command hangs on unreachable systems without the -G option. If that does not work use the Workaround.

Nc:nc 通常已经安装,但是在某些系统(例如 Mac OS X)上,如果没有 -G 选项,该命令会在无法访问的系统上挂起。如果这不起作用,请使用解决方法。

nc -v -z -w 3 127.0.0.1 22 &> /dev/null && echo "Online" || echo "Offline"

Mac OSX:

Mac OSX:

nc -z -G 3 127.0.0.1 22  &> /dev/null && echo "Online" || echo "Offline"

Alternative workaround option:

替代解决方法选项:

bash -c '(sleep 3; kill $$) & exec nc -z 127.0.0.1 22' &> /dev/null
echo $?
0
bash -c '(sleep 3; kill $$) & exec nc -z 1.2.3.4 22' &> /dev/null
echo $?
143

(examples illustrate connecting to port 22 ssh over a good and bad host example, use the $? to determine if it reached the host with the sleep time of 3 seconds)

(示例说明通过一个好的和坏的主机示例连接到端口 22 ssh,使用 $? 确定它是否以 3 秒的睡眠时间到达主机)

For Mac Users (mainly) etc, you can use the command in the script like so :

对于 Mac 用户(主要是)等,您可以在脚本中使用命令,如下所示:

    # -- use NMAP, if not avail. go with nc --
    if command -v nmap | grep -iq nmap ; then
        nmap ${ip} -PN -p ${ssh_port} | grep -iq "open"
        res=$?
    elif command -v nc | grep -iq nc ; then
        # -- run command if fails to complete in 3 secs assume host unreachable --
        ( nc -z ${ip} ${ssh_port} ) & pid=$!
        ( sleep 3 && kill -HUP $pid ) 2>/dev/null & watcher=$!
        if wait $pid 2>/dev/null; then
            pkill -HUP -P $watcher
            wait $watcher
            # -- command finished (we have connection) --
            res=0
        else
            # -- command failed (no connection) --
            res=1
        fi
    else
        echo "Error: You must have NC or NMAP installed"
    fi

    if [[ ${res} -lt 1 ]] ;then
        success=1
        echo "testing  => $ip SUCCESS connection over port ${ssh_port}"
        break;
    else
        echo "testing => $ip FAILED connection over port ${ssh_port}"
    fi

回答by Pierfrancesco PierQR Aiello

On Mac OS X (10.14.6) the -w 3parameter is somehow ignored.

在 Mac OS X (10.14.6) 上,该-w 3参数以某种方式被忽略。

The workaround I found is: timeout 3 nc -vz 10.18.50.134 23

我找到的解决方法是: timeout 3 nc -vz 10.18.50.134 23

回答by MacTroy

Since Mac OS X 10.8.x, nchas used the -Goption to set the timeout for initiating a connection. This is separate from the -woption, which sets the timeout for a connection that has been made but has gone idle.

从 Mac OS X 10.8.x 开始,nc已使用该-G选项来设置启动连接的超时时间。这与-w设置已建立但已空闲的连接的超时的选项不同。

If you are trying to use ncfor port scanning, i.e. nc -v -z 10.0.1.1 1-1023, it will spend over a minute trying to connect to each non-responding port unless you add a -Gtimeout value:

如果您尝试nc用于端口扫描,即nc -v -z 10.0.1.1 1-1023,除非您添加-G超时值,否则它将花费超过一分钟的时间尝试连接到每个无响应的端口:

nc -v -z -G 1 10.0.1.1 1-1023

nc -v -z -G 1 10.0.1.1 1-1023

That's one second per port scanned — much more useful.

这是每个端口扫描一秒钟 - 更有用。