Netcat - 如何获得连接时间?

时间:2017-04-10 07:27:24

标签: ubuntu netcat

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

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

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

3 个答案:

答案 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

参考:https://serverfault.com/questions/649532/what-is-the-best-way-to-measure-latency-via-a-telnet-to-port-test-preferably