我使用简单的代码来测试一些服务:
nc -v -w 2 google.com 80
Connection to google.com 80 port [tcp/http] succeeded!
它工作得很好,但我还需要一个连接时间。是否可以通过netcat获取它?我在手册中找不到这个:(
这是你想要的吗?
echo "[$(date)] Connection to $SERVER $PORT port [tcp/http] $(nc -z $SERVER $PORT &> /dev/null && echo succeeded || echo failed)"
例:
$ echo "[$(date)] Connection to google.com 80 port [tcp/http] $(nc -z google.com 80 &> /dev/null && echo succeeded || echo failed)"
[Mon Apr 10 12:40:31 UTC 2017] Connection to google.com 80 port [tcp/http] succeeded
$ echo "[$(date)] Connection to google.com 80 port [tcp/http] $(nc -z google2.com 80 &> /dev/null && echo succeeded || echo failed)"
[Mon Apr 10 12:40:20 UTC 2017] Connection to google.com 80 port [tcp/http] failed
如果你使用ncat(用nmap打包)而不是netcat,你可以获得如下的连接计时信息:
ncat -v -z google.com 80
输出将显示如下:
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Connected to 172.217.15.78:80.
Ncat: 0 bytes sent, 0 bytes received in 0.35 seconds.