我使用简单的代码来测试某些服务:
nc -v -w 2 google.com 80
Connection to google.com 80 port [tcp/http] succeeded!
它工作得很好,但我还需要一个连接时间。是否可以通过netcat获取它?我在手册中找不到这个:(
答案 0 :(得分:0)
这是你想要的吗?
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
答案 1 :(得分:0)
如果使用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.
答案 2 :(得分:0)
time nc -zw30 <host> <port>
// example:
$ time nc -zw30 google.com 443
Connection to google.com port 443 [tcp/https] succeeded!
real 0m0.064s
user 0m0.003s
sys 0m0.006s