14.2 验证计算机是否正在运行和能否接收请求

ping

ping –c

ping命令能够向指定的IP地址发送一种特殊的数据包(ICMP ECHO_REQUEST消息)。如果那个地址上的计算机正在监听ICMP消息,它将用ICMP ECHO_REPLY数据包做出响应(确实,防火墙可以阻止ICMP消息,使ping无效,但大多数时候这并不是问题)。如果ping响应成功,则意味着两台计算机之间的网络是连通的。

  1. $ ping www.google.com
  2. ping www.google.com
  3. PING www.l.google.com (72.14.203.99) 56(84) bytes of data.
  4. 64 bytes from 72.14.203.99: icmp_seq=1 ttl=245 time=17.1 ms
  5. 64 bytes from 72.14.203.99: icmp_seq=2 ttl=245 time=18.1 ms
  6. [Results truncated for length]--- www.l.google.com
  7. ping statistics ---
  8. 6 packets transmitted, 5 received, 16% packet loss, time 5051ms
  9. 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发送的数据包个数达到指定的数量后,就会停止,并报告结果。

  1. $ ping -c 3 www.granneman.com
  2. PING granneman.com (216.23.180.5) 56(84) bytes of data.
  3. 64 bytes from 216.23.180.5: icmp_seq=1 ttl=44 time=65.4 ms
  4. 64 bytes from 216.23.180.5: icmp_seq=2 ttl=44 time=64.5 ms
  5. 64 bytes from 216.23.180.5: icmp_seq=3 ttl=44 time=65.7 ms
  6. --- granneman.com ping statistics ---
  7. 3 packets transmitted, 3 received, 0% packet loss,
  8. time 4006ms
  9. rtt min/avg/max/mdev = 64.515/65.248/65.700/0.510 ms

对于判断基本的网络连通性,ping命令是一种标准而又快速的方法。而且使用了–c选项,效果会更好,这样就再也不会忘记关闭它,让ping意外地连续运行18天了。

有关使用ping来诊断网络连接故障的更多信息,可以参考“网络问题疑难排解”一节。