ping 命令能够帮助我们测试两台主机之间的网络是否畅通。但是,当目标主机的 ICMP 协议端口都关闭的时候,无法通过 ping 命令达到目标。一台主机只要是需要连接服务,那么它一定会开启 tcp 协议的某个端口,因此,我们可以通过基于 tcp 协议的 ping 命令 tcping 来获知两台主机是否网络联通。本篇以 Ubuntu 18.04 为例介绍如何安装 TCPing,以及如何使用它探测网络是否联通。
安装 TCPing
| 12
 3
 4
 5
 
 | sudo apt update
 sudo apt install tcptraceroute wget bc
 sudo wget http://www.vdberg.org/\~richard/tcpping -O /usr/bin/tcping
 sudo chmod +x /usr/bin/tcping
 
 | 
使用 TCPing
使用方法:
| 12
 3
 4
 5
 6
 7
 8
 
 | Usage: tcping [-d] [-c] [-C] [-w sec] [-r secs] [-q num] [-x count] ipaddress [port]-d   print timestamp before every result
 -c   print a columned result line
 -C   print in the same format as fping's -C option
 -w   wait time in seconds (defaults to 3)
 -r   repeat every n seconds (defaults to 1)
 -q   query numbers (defaults to 1)
 -x   repeat n times (defaults to unlimited)
 
 | 
例子:
| 12
 3
 4
 5
 6
 7
 8
 
 | tcping www.baidu.com 443
 tcping www.wshifen.com 443
 
 tcping www.baidu.com 80
 tcping www.baidu.com
 
 tcping 45.113.192.102
 
 | 
如果网络端口是通的,将会打印链接时间,如下面示例:
| 12
 3
 
 | seq 0: tcp response from 45.113.192.102 [open]  1.918 msseq 1: tcp response from 45.113.192.101 [open]  1.659 ms
 seq 2: tcp response from 45.113.192.102 [open]  1.651 ms
 
 | 
如果网络端口不通,则会打印下面示例:
| 12
 3
 
 | seq 0: no response (timeout)seq 1: no response (timeout)
 seq 2: no response (timeout)
 
 | 
更多关于端口探测方法请查看我的另一篇博文:测试 Linux 某个端口是否打开
参考文献
- Debian/Ubuntu安装TCP测试工具TCPing