Netcat - 如何获得连接时间?

问题描述 投票:0回答:2

我使用简单的代码来测试一些服务:

nc -v -w 2 google.com 80
Connection to google.com 80 port [tcp/http] succeeded!

它工作得很好,但我还需要一个连接时间。是否可以通过netcat获取它?我在手册中找不到这个:(

ubuntu netcat
2个回答
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

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.
© www.soinside.com 2019 - 2024. All rights reserved.