初学者入门:Ping–Unix/Linux命令

译文
系统
了解 ping ,并查看介绍及其工作原理、默认 TTL、常用 ping 命令等。

【51CTO.com快译】“ping” 命令用于排除和诊断网络连接问题,并检查主机是否可访问,它适用于所有操作系统。

“ping”命令的工作原理

Ping 数据包工作原理图

Ping 命令从“主机 A”(源)向“主机 B”(目标)发送一个 32 字节的 ICMP(互联网控制消息协议)数据包,并等待“主机 B”向“主机 A”响应一个 32 字节的数据包”。如果连接成功,则打印整个往返时间,如果不成功,则打印失败原因,诸如丢包、超时、连接错误等。

如何执行“ping”命令?

以下是调用“ping”命令的方法:

  1. ping [IP/domain] 

例子:

  1. ping gceasy.com 

执行上述命令后,控制台会打印以下消息:

  1. Pinging gceasy.com [195.201.17.137] with 32 bytes of data:  
  2. Reply from 195.201.17.137: bytes=32 time=217ms TTL=45  
  3. Reply from 195.201.17.137: bytes=32 time=185ms TTL=45  
  4. Reply from 195.201.17.137: bytes=32 time=188ms TTL=45  
  5. Reply from 195.201.17.137: bytes=32 time=196ms TTL=45  
  6.  
  7. Ping statistics for 195.201.17.137:     
  8.     Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),  
  9. Approximate round trip times in milli-seconds:     
  10.     Minimum = 185ms, Maximum = 217ms, Average = 196ms 

• 时间:这是往返时间。它是数据包从源地址到达目的地并报告回源地址所花费的时间

• TTL(生存时间):是时间或“跳数”的量,它可以从 0 到 255 不等,具体取决于主机操作系统。当数据包通过互联网发送时,数据包有可能从路由器通过,并且数据包将被发送,直到用户中断此命令。数据包设计了一个称为生存时间的过期时间。

• Ping statistics:

    Packets Sent(发送的数据包): 数据包从主机发送到目的地的次数。

    Packets Received(收到的数据包) : 从目的地到主机收到的数据包的次数。

    Packets Lost(数据包丢失) : 无法到达目的地的数据包数。

• 大约往返时间:

    最小值:以毫秒为单位获得响应的最短时间

    平均:以毫秒为单位获得响应的平均时间

    最大:以毫秒为单位获得响应的最长时间

 Ping 的默认 TTL(生存时间) 是多少?

    Ping 命令的 TTL值因操作系统而异。下表总结了ping命令对于常用操作系统的默认TTL

Ping 错误:

• 请求超时:如果由于网络流量过大,主机没有响应,或者 ARP 请求包过滤失败,或者如果路由器出现错误,则 ping 请求将超时。

例子:

  1. ping gceasy.io  
  2. Pinging gceasy.io [52.52.12.54] with 32 bytes of data:  
  3. Request timed out.  
  4. Request timed out.  
  5. Request timed out.  
  6. Request timed out.  
  7.  
  8. Ping statistics for 52.52.12.54:     
  9.       Packets: Sent = 4, Received = 0, Lost = 4 (100% loss) 

解决方案:使用 ping -w 开关增加等待时间。

• Ping 请求无法找到主机:当无法根据传递给 ping 命令的主机名确定 IP 地址时,将遇到此错误。 

例子:

  1. ping test123blah.com  
  2. Ping request could not find host test123blah.com. Please check the name and try again. 

解决方案:验证传递给 ping 命令的主机名。验证 DNS 服务器是否可用/可从主机访问。

• 目标主机无法访问:当访问的主机停机或在网络上不可用时,将返回“目标主机无法访问”错误。 

例子:

  1. ping 192.168.0.0  
  2. Pinging 192.168.0.0 with 32 bytes of data:  
  3. Reply from 192.168.0.102: Destination host unreachable.  
  4. Reply from 192.168.0.102: Destination host unreachable.  
  5. Reply from 192.168.0.102: Destination host unreachable.  
  6. Reply from 192.168.0.102: Destination host unreachable.  
  7.  
  8. Ping statistics for 192.168.0.0:     
  9.        Packets: Sent = 4, Received = 4, Lost = 0 (0% loss) 

解决方案:验证目标主机是否已启动并正在运行。修改本地路由表或通知路由器管理员。

    • TTL 在传输过程中过期: 当将数据包路由到目的地时出现重循环时,TTL 将过期。这将导致“TTL在传输中过期”错误。

解决方案: 确定重复并导致循环的 IP 地址。在 Linux 问题上,以 root 用户身份使用“route -n”命令查看当前路由表,在 Windows 问题上,命令输出“route print”。

• Ping命令:传输失败。

    一般故障:当设备网卡或无线适配器存在物理问题时,将导致“Ping:transmit failed. ”(Ping:传输失败)、“一般失败'错误。

例子:

  1. ping 2DAB:FFFF:0000:3EAE:01AA:00FF:DD72:2C4A  
  2.  
  3. Pinging 2dab:ffff:0:3eae:1aa:ff:dd72:2c4a with 32 bytes of data:  
  4. PING: transmit failed. General failure.  
  5. PING: transmit failed. General failure.  
  6. PING: transmit failed. General failure.  
  7. PING: transmit failed. General failure.  
  8.  
  9. Ping statistics for 2dab:ffff:0:3eae:1aa:ff:dd72:2c4a:     
  10.        Packets: Sent = 4, Received = 0, Lost = 4 (100% loss) 

解决方案:确保网卡没有问题。确保设备上安装了最新版本的网络驱动程序,检查是否为设备分配了正确的 IP 地址。

常用的 Ping 命令

可以在使用 'ping' 命令的同时使用其它选项来获取要查找的信息。

发送更多请求,请执行以下操作:

如果要发送 '5' 回显请求,则可以发出以下命令:

  1. ping -n 5 gceasy.com 

执行上述命令后,控制台会打印以下消息:

  1. Pinging gceasy.com [195.201.17.137] with 32 bytes of data:  
  2. Reply from 195.201.17.137: bytes=32 time=168ms TTL=45  
  3. Reply from 195.201.17.137: bytes=32 time=165ms TTL=45  
  4. Reply from 195.201.17.137: bytes=32 time=170ms TTL=45  
  5. Reply from 195.201.17.137: bytes=32 time=170ms TTL=45  
  6. Reply from 195.201.17.137: bytes=32 time=183ms TTL=45  
  7.  
  8. Ping statistics for 195.201.17.137:     
  9.      Packets: Sent = 5, Received = 5, Lost = 0 (0% loss),  
  10. Approximate round trip times in milli-seconds:     
  11.      Minimum = 165ms, Maximum = 183ms, Average = 171ms 

要Ping指定主机直到停止,请执行以下操作:

如果想向指定的主机发送请求,直到手动停止为止,则可以发出以下命令:

  1. ping -t gceasy.com 

Ping 工具

性能根本原因分析工具(如yCrash)捕获应用程序级工件(如线程转储、堆转储、垃圾收集日志)和系统级工件(如 netstat、vmstat、iostat、top、top -H、dmesg 等)。除此之外,该工具还捕获 ping 命令输出。工具分析所有工件,并立即生成统一的根本原因分析报告。如果该工具在 ping 命令输出中发现任何错误,它将在报告中发出警报。

图: yCrash工具中捕获并报告的 ping 输出

'ping' 命令选项 

如果想了解传递给“ping”命令的所有选项,请参阅此处

【51CTO译稿,合作站点转载请注明原文译者和出处为51CTO.com】

责任编辑:梁菲 来源: DZone
相关推荐

2009-08-30 15:04:56

2018-02-10 07:36:20

Linux命令wc

2018-02-25 14:00:07

Linux命令whereis

2018-02-23 10:03:30

Linux命令mkdir

2018-04-08 14:47:06

Linux命令type

2018-03-12 19:43:48

Linux命令 ldd

2009-11-18 09:30:43

2011-08-24 17:05:01

Lua

2020-08-16 13:10:46

TensorFlow深度学习数据集

2022-09-05 15:36:39

Linux日志记录syslogd

2018-02-24 10:38:06

Linux命令rmdir

2022-11-16 16:02:59

Linux命令

2011-03-02 12:43:40

vsFTPd

2011-09-16 09:38:19

Emacs

2022-04-24 15:21:01

MarkdownHTML

2011-04-12 10:13:24

2014-03-12 10:11:57

Python设计模式

2009-06-05 11:01:07

淘宝Open API入门教程

2010-11-24 16:15:09

UI设计Windows Pho
点赞
收藏

51CTO技术栈公众号