bash 如何使用netcat等待开放端口?

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

How to wait for an open port with netcat?

bashshellnetcat

提问by FXG

I'm trying to do a custom dockerfile with jenkins on it. I would to wait until port 8080 is open instead of doing an ugly 'sleep 60' with netcat but in not very confident with bash scripts and netcat.

我正在尝试使用 jenkins 做一个自定义的 dockerfile。我会等到端口 8080 打开,而不是用 netcat 做一个丑陋的“睡眠 60”,但对 bash 脚本和 netcat 不是很自信。

Here is an example of what i'm trying to do:

这是我正在尝试做的一个例子:

#!/bin/bash

opened=0

while [ "$opened"  == "0" ]; do
  echo "Waiting jenkins to launch on 8080..."
  nc -vz localhost 8080
done

echo "Jenkins launched"

回答by user987339

You can't set netcat to wait until some port is open, so you have to add part for waiting before next check is made. Try this:

您不能将 netcat 设置为等到某个端口打开,因此您必须在下一次检查之前添加等待部分。尝试这个:

#!/bin/bash

echo "Waiting jenkins to launch on 8080..."

while ! nc -z localhost 8080; do   
  sleep 0.1 # wait for 1/10 of the second before check again
done

echo "Jenkins launched"

回答by Onlyjob

I suggest the following one liners:

我建议使用以下一种衬垫:

## netcat version:
timeout 22 sh -c 'until nc -z 
#!/bin/bash

echo "Waiting jenkins to launch on 8080..."

while ! timeout 1 bash -c "echo > /dev/tcp/localhost/8080"; do   
  sleep 1
done

echo "Jenkins launched"
; do sleep 1; done' stackoverflow.com 443 ## pure bash version: timeout 22 bash -c 'until printf "" 2>>/dev/null >>/dev/tcp/
# without timeout
wait-port localhost:8080

# timeout after a minute
wait-port -t 60000 localhost:8080
/; do sleep 1; done' stackoverflow.com 443

Both commands exit as soon as connection is established, trying every second for up to 22 seconds.

一旦建立连接,这两个命令都会退出,每秒尝试一次,最多 22 秒。

Note that thanks to timeoutcommand exit code is 0when port is accessible otherwise 124(if no connection established within given time).

请注意,多亏了timeout命令退出代码0,否则端口可访问124(如果在给定时间内未建立连接)。

回答by Anthony O.

As suggested here, you could also do the following if you don't have ncinstalled but just bashand coreutils:

正如此处所建议的,如果您还没有nc安装而只是安装了bash和,您也可以执行以下操作coreutils

waitport() {
    while ! nc -z localhost  ; do sleep 1 ; done
}

回答by Dave Kerr

I have found this a common enough problem to write a utility to wait for a port to open, with an optional timeout:

我发现这是一个足够常见的问题,可以编写一个实用程序来等待端口打开,并带有可选的超时:

$ waitport 3000
Connection to localhost port 3000 [tcp/hbci] succeeded!

It's open source and available at github.com/dwmkerr/wait-port. Hopefully others will find it useful!

它是开源的,可从github.com/dwmkerr/wait-port 获得。希望其他人会发现它有用!

回答by johntellsall

To expand on user987339's answer, here's how to easily wait for a port in your terminal:

要扩展 user987339 的回答,以下是在终端中轻松等待端口的方法:

waitport function

等待端口功能

Add this function to your ~/.bashrc setup file:

将此函数添加到您的 ~/.bashrc 安装文件中:

##代码##

Log out then back in to load ~/.bashrc. Then, run this command to verify that port 3000 has a server listening to it:

注销然后重新登录以加载 ~/.bashrc。然后,运行此命令以验证端口 3000 是否有服务器侦听它:

##代码##

This has been validated on macOS. It might not work on Fedora/CentOS, as they lack the -zoption for netcat.

这已在 macOS 上得到验证。它可能无法在Fedora / CentOS的工作,因为他们缺乏-z的选项netcat