14.2 验证计算机是否正在运行和能否接收请求
ping
ping –c
ping
命令能够向指定的IP地址发送一种特殊的数据包(ICMP ECHO_REQUEST消息)。如果那个地址上的计算机正在监听ICMP消息,它将用ICMP ECHO_REPLY数据包做出响应(确实,防火墙可以阻止ICMP消息,使ping
无效,但大多数时候这并不是问题)。如果ping
响应成功,则意味着两台计算机之间的网络是连通的。
$ ping www.google.com
ping www.google.com
PING www.l.google.com (72.14.203.99) 56(84) bytes of data.
64 bytes from 72.14.203.99: icmp_seq=1 ttl=245 time=17.1 ms
64 bytes from 72.14.203.99: icmp_seq=2 ttl=245 time=18.1 ms
[Results truncated for length]--- www.l.google.com
ping statistics ---
6 packets transmitted, 5 received, 16% packet loss, time 5051ms
rtt min/avg/max/mdev = 16.939/17.560/18.136/0.460 ms
直到按下Ctrl+C组合键,ping
命令才会停下来。如果忘记了还在使用ping
命令,这可能会导致问题,因为它将一直运行下去,直到它被停止或计算机的网络连接中止。我曾经有一次忘记关闭ping
,让它连续运行了18天,向我的一个服务器发送了近1.4百万条ping
消息。哎,真是糟糕啊!如果想给ping
加一些限制,仅发送一定数量的数据包,则可以使用-c
选项,后面跟上一个数字。在ping
发送的数据包个数达到指定的数量后,就会停止,并报告结果。
$ ping -c 3 www.granneman.com
PING granneman.com (216.23.180.5) 56(84) bytes of data.
64 bytes from 216.23.180.5: icmp_seq=1 ttl=44 time=65.4 ms
64 bytes from 216.23.180.5: icmp_seq=2 ttl=44 time=64.5 ms
64 bytes from 216.23.180.5: icmp_seq=3 ttl=44 time=65.7 ms
--- granneman.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss,
time 4006ms
rtt min/avg/max/mdev = 64.515/65.248/65.700/0.510 ms
对于判断基本的网络连通性,ping
命令是一种标准而又快速的方法。而且使用了–c
选项,效果会更好,这样就再也不会忘记关闭它,让ping
意外地连续运行18天了。
有关使用ping
来诊断网络连接故障的更多信息,可以参考“网络问题疑难排解”一节。