PING - The Easy Tutorial

Networking Ping
最近更新: Sep 23 2010


Chinese translation by Jie Yu.




If you like our tutorials, don't hesitate to support us and visit our sponsors!
Si vous aimez nos tutoriaux, n'hésitez pas à nous supporter et visiter nos sponsors!


Ping – 目录

简介
    互联网控制协议单元(ICMP Packet)介绍
    Ping的原理
使用实例
输出结果分析
    目标可连接性
    网络拥堵状况(RTT)
    生存周期(TTL)
ICMP数据包详细分析
    Echo Request(回声请求)
    Echo Response(回声响应)
PING的参数
    Windows
    Linux
ICMP头(HEADER)
IP地址与主机名称



简介

Ping是用于检查两台IP设备间连通性的常用工具,由Mike Muuss于1983年开发完成。其作者在2000年完成著作《Ping的故事》 ( The Story of the PING Program) 介绍这一著名工具后,不幸在一场车祸中丧生。

Ping作为默认工具被预装在Windows,Apple以及各种Linux/Unix操作系统中。
它基于互联网控制协议(以下简称ICMP):这是一种专门用来检查IP网络连通性以及在其中获取其它设备信息的协议。
ICMP被封装在IP封包(IP packet)中,但通常被认为是IP或Internet层的一部分。

互联网控制协议单元(ICMP Packet)介绍

icmp paceket structure overview

Ping的原理

Ping向目标发设备发送很小的数据包,而目标设备会将收到的数据包发回。
向目标设备发送的ICMP数据包被称为echo_request(回声_请求),而之后被返回的数据包则叫做echo_response(回声_响应)。
还有许多其它的
ICMP数据包类型,我们将在本页稍后介绍它们。

ping picture

返回页面顶端



使用实例

让我们分析一个实例。两台计算机,分别叫做Paris和Berlin,用它们ping另一台计算机"www.google.ch"。
Wireshark嗅探器(sniffer)被安装在Berlin与"www.google.ch"之间。它将被用于捕获数据包的内容。
Paris运行Ubuntu Linux系统,Berlin运行Microsoft XP 。

ping tutorial scenario

从Linux(Paris) Ping "www.google.ch":

Paris: ping www.google.ch

PING www.google.ch (209.85.135.105) 56(84) bytes of data.
64 bytes from www.google.ch(209.85.135.105): icmp_seq=1 ttl=255 time=1.19 ms
64 bytes from www.google.ch (209.85.135.105): icmp_seq=2 ttl=255 time=1.25 ms
64 bytes from www.google.ch (209.85.135.105): icmp_seq=3 ttl=255 time=1.26 ms
64 bytes from www.google.ch (209.85.135.105): icmp_seq=4 ttl=255 time=1.29 ms

--- www.google.ch ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 1.192/1.250/1.290/0.044 ms
从Windows (Berlin) Ping "www.google.ch":

Berlin: ping www.google.ch

Pinging www.google.ch [209.85.135.105] with 32 bytes of data:

Reply from 209.85.135.105: bytes=32 time=18 ms TTL=250
Reply from 209.85.135.105: bytes=32 time=21 ms TTL=250
Reply from 209.85.135.105: bytes=32 time=20 ms TTL=250
Reply from 209.85.135.105: bytes=32 time=33 ms TTL=250

Ping statistics for 209.85.135.105:
      Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
      Minimum = 18ms, Maximum = 33ms, Average = 23ms
我们能从以上结果中得到什么信息?

- 目标是否在运行?=>远程目标的可连接性
- 网络速度怎么样?=>网络拥堵程度
- 目标离我远吗?=>连接距离

请参考下一节"输出结果分析"以完整了解如何理解Ping命令的返回结果。

我们可以说,不论安装了何种操作系统,Ping为我们提供的信息始终都是相同的。但在不同的操作系统中,Ping命令的默认设置仍然会有细微的差别。请参考"Ping的参数"一节以详细了解。

让我们看一下Wireshark捕获的数据(详细分析请参考"详细分析"一节)。

ping tutorial scenario

Wireshark所捕获的内容为我们提供了如下信息:

- 数据包被识别为ICMP数据包。 协议栏(Protocol)
- 发送(echo_request)及接收(echo_reply)的ICMP数据包数量均为4。信息栏(info)
- 数据包长度为74 bytes,其中包含“消息头”(headers, 42 bytes) 以及ICMP数据(data, Windows默认值为32 bytes)。长度栏(length column)

返回页面顶端



输出结果分析

就像我们之前提到的,Ping主要为我们提供了三个方面的信息:

目标可连接性
网络拥堵状况
生存周期(Time To Live)

目标可连接性

在之前的例子中,我们发出的4条Ping请求都得到了回应。这样的结果表明目标设备在ICMP层运行正常。不过另一方面,我们并不能就此推断其它服务,比如webserver,是否也运行正常。

如果我收到了失败的结果,那说明什么?
让我们看一个例子:
C:\>ping www.openmaniak.com

Pinging openmaniak.com [84.16.88.15] with 32 bytes of data:

Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 84.16.88.15:
      Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

远程主机www.openmaniak.com,其IP地址为84.16.88.15,并没有对我们的Ping请求产生回应。有必要提醒,失败的结果并不总是表明对方没有正常运行(在我们的例子中,它正在正常运行一个webserver)。在这种情况下,这样的结果只说明远程主机并不回应ICMP请求。

"没有响应"的原因都有哪些?
并不是所有的"没有响应"都能简单的找出问题的根源。事实上,造成这种现象的原因有很多。在接下来的图片里,我们将会总结四种最常见的情况。

ping stop scenario

第一种情况,外部防火墙阻断了ICMP请求。ICMP可以被用作网络攻击的第一步,因为它可以判断远程主机是否正在运行。在这种情况下,尽管一切运行正常,但由于防火墙的存在,它们对外界呈现不可见状态。
阻挡ICMP消息通常是在组建安全网络时得到的第一条建议。由于外部防火墙非常昂贵而且需要专业人员进行配置,因此它们一般被用来保护专业级别的网络。

第二种情况,ICMP消息被安装运行在工作站上的个人防火墙阻挡。基于与第一种情况相同的原因,个人防火墙被推荐安装在家用电脑上。

第三种情况,被Ping的计算机平没有接入网络,比如网线被拔出。
echo_request消息将会在到达目标前被最后一级的路由器丢弃。

第四种情况,目标设备被关闭或者网卡被禁用。就像上一种情况一样,echo_request消息将会在到达目标前被最后一级的路由器丢弃。



如果一台叫做Geneva的设备可以成功Ping另一台设备Dallas,那么如果反过来,由Dallas Ping Geneva,一定会成功吗?
答案是否定的。正如下图所示,你可以设置防火墙只阻挡一个方向的echo_request。

当Dallas Ping Geneva时,ICMP echo_request请求会被防火墙阻挡,所以Dallas无法接收到Geneva的响应。

但当Geneva Ping Dallas时,ICMP echo_request则能够被Dallas收到,而且Geneva也能够成功接收echo_response,所以Ping的结果是成功的。在这种情况下,防火墙只阻挡从外界发送进来的echo_request,并不阻止其他ICMP数据包。而在我们的例子中,echo_request刚好是从内部向外发送,从外部发回的只有echo_response消息。

ping stop scenario

返回页面顶端



网络拥堵状况

Ping所提供的另一项极有价值的信息是从发出请求到收到响应所消耗的时间。这项指标叫做"往返时间" (RTT, Round Trip Time)或者"响应时间" (response time)。它的单位是毫秒。
请不要把这个概念与"迟延"(latency)或"延时"(delay)混淆起来,后两者的定义是数据包穿过网络,到达目标所用的时间,是单向的。

"响应时间"是影响网络应用程序性能的重要参数。高响应时间的结果是性能下降。
当网络应用程序运行缓慢时,第一项要检查的就是客户端与服务器之间的响应时间,看网络连接状况是不是造成问题的原因。

同时,我们也有必要注意另一项与响应时间有关的信息:数据包丢失率(丢包率)。如果一个ICMP数据包在传送途中被丢弃或未能在默认的超时时间,2秒内返回,就会被认为已经丢失。数据包丢失会提高系统的TCP重发率(retransmission rate),从而使网络应用程序运行缓慢或经常被打断。
在局域网内,数据包丢失的现象应该并不常见。



哪些因素会影响响应时间和数据包丢失率?

- 网络线缆
网线质量和种类对响应时间有着很大的影响。比如,光纤会比老式的铜芯线快很多。质量低劣或受损的线缆会造成数据包丢失。宽导线同样有助于降低响应时间,特别是在高数据流量的时候。这与汽车堵车是一个道理。

- 网络设备
网络设备离你越远,相应时间就会越长。事实上,就算使用最理想的线材和路由器,物理距离和你与目标之间路由器的数量都显然会对响应时间产生影响。

- 物理距离
网络设备离你越远,相应时间就会越长。事实上,就算使用最理想的线材和路由器,物理距离和你与目标之间路由器的数量都显然会对响应时间产生影响。
这是大概的数值:在局域网环境中,响应时间大约为1或2毫秒;而在一个距你非常远的VPN网络中,响应时间可能会高达300毫秒。 下面用一个例子来说明:
从瑞士日内瓦向Google瑞士服务器发送的Ping请求会在30毫秒左右得到相应,但如果改成Google在日本的服务器,这个时间将会变成130毫秒左右。

- 源与目标设备
人们通常认为高响应时间或丢包率是网络问题造成的,但实际上并非如此。很多时候,出现这样的现象都是因为源设备或目标设备过于繁忙,或者网卡出现了问题。
Ping命令输出的信息向我们提供了最低,平均以及最高的响应时间,帮助我们了解在这一段时间内响应时间究竟是保持平稳还是在剧烈变化。

返回页面顶端



生存周期(Time-to-Live)

生存周期(TTL, Time-To-Live)可以使你了解你与目标之间路由的数量。
TTL是为了防止IP数据包在网络中无休止的循环,造成网络过载而实际的。
对于IP数据包,TTL的初始值是255,每经过一个路由器,这个值就会被减一。当TTL为0的时候,数据包就会被路由器丢弃。所有的IP数据包,包括ICMP数据包都包含TTL。Ping命令给出的TTL值实际上是echo_response消息的。
在默认设施下,Windows会把TTL降低(减去)128,Ubuntu Linux则降低192。

让我们研究三个A Ping B的场景。B将会作为路由器(场景一),装有Microsoft Windows的计算机(场景二)以及装有Ubuntu Linux的计算机(场景三)。
TTL的初始值是255,之后会操作系统降低,就像之前描述的一样。

在"详细分析"一节中,我们将会讨论TTL在IP数据包中的具体位置。

场景一:
当A Ping B时,A收到的消息TTL为251,因为数据包经过了4台路由器(-4)。
TTL=255-4=251.

openmaniak scenario ttl time-to-live router

ping B

Pinging B [1.1.1.1] with 32 bytes of data:

Reply from 1.1.1.1: bytes=32 time=18 ms TTL=251
Reply from 1.1.1.1: bytes=32 time=21 ms TTL=251
Reply from 1.1.1.1: bytes=32 time=20 ms TTL=251
Reply from 1.1.1.1: bytes=32 time=33 ms TTL=251

Ping statistics for 1.1.1.1:
      Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
      Minimum = 18ms, Maximum = 33ms, Average = 23ms
场景二:
当A Ping B时,A收到的消息TTL为124,因为数据包经过了3台路由器(-3)及一台Windows计算机(-128)。
TTL=255-3-128=124.

openmaniak scenario ttl time-to-live microsoft windows

ping B

Pinging B [1.1.1.1] with 32 bytes of data:

Reply from 1.1.1.1: bytes=32 time=18 ms TTL=125
Reply from 1.1.1.1: bytes=32 time=21 ms TTL=125
Reply from 1.1.1.1: bytes=32 time=20 ms TTL=125
Reply from 1.1.1.1: bytes=32 time=33 ms TTL=125

Ping statistics for 1.1.1.1:
      Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
      Minimum = 18ms, Maximum = 33ms, Average = 23ms
场景三:
当A Ping B时,A收到的消息TTL为62,因为数据包经过了3台路由器(-3)及一台运行Ubuntu Linux的计算机(-192)。
TTL=255-3-192=60.

openmaniak scenario ttl time-to-live ubuntu linux

ping B

Pinging B [1.1.1.1] with 32 bytes of data:

Reply from 1.1.1.1: bytes=32 time=18 ms TTL=60
Reply from 1.1.1.1: bytes=32 time=21 ms TTL=60
Reply from 1.1.1.1: bytes=32 time=20 ms TTL=60
Reply from 1.1.1.1: bytes=32 time=33 ms TTL=60

Ping statistics for 1.1.1.1:
      Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
      Minimum = 18ms, Maximum = 33ms, Average = 23ms
返回页面顶端



详细分析

这一节中,我们将会详细分仔细echo_request和echo_response数据包中的内容。

openmaniak scenario ttl time-to-live ubuntu linux

Echo request (回声请求)

下面第一幅图是Wireshark软件的截屏,第二幅图是其捕获到信息的文字内容。

ping tutorial scenario

No.
Time
  
Source   
Destination  Protocol length Info
1
0.000000
192.168.1.100 209.85.135.105 ICMP 74 Echo (ping) request








Frame 1 (74 bytes on wire, 74 bytes captured)
Ethernet II, Src: Dell_11:11:11 (00:19:b9:11:11:11), Dst: ThomsonT_99:99:99 (00:18:f6:99:99:99)
Internet Protocol, Src: 192.168.1.100 (192.168.1.100), Dst: 209.85.135.105 (209.85.135.105)

Version: 4

Header length: 20 bytes

Differentiated Services Field: 0x00 (DSCP 0x00: Default; ECN: 0x00)


0000
00..
= Differentiated Services Codepoint: Default (0x00)


....
..0.
= ECN-Capable Transport (ECT): 0


....
...0
= ECN-CE:  0

Total Length: 60

Identification: 0x5671 (22129)

Flags: 0x00


 0..
= Reserved bit: Not Set


.0.
= Don't fragment: Not Set


..0
= More fragments: Not Set

Fragment offset: 0

Time to live: 128

Protocol: ICMP (0x01)

Header checksum: 0xc984 [correct]


[Good: True]


[Bad : False]

Source: 192.168.1.100 (192.168.1.100)

Destination: 209.85.135.105 (209.85.135.105)
Internet Control Message Protocol

Type: 8 (Echo (ping) request)

Code: 0 ()

Checksum: 0x4a5c [correct]

Identifier: 0x0200

Sequence number: 256 (0x0100)

Data (32 bytes)








0000
61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70
abcdefghijklmnop
0010 71 72 73 74 75 76 77 61 62 63 64 65 66 67 68 69
qrstuvwabcdefghi



Data: 6162636465666768696A6B6C6D6E6F707172737475767761...



[Length: 32]

- Wireshark截图(第一幅图)中显示的是 不同OSI层 (Ethernet - IP - IMCP)中的信息。请参考章节"互联网控制协议单元(ICMP Packet)介绍"。
- 数据包在IP层被识别为ICMP。
- ICMP层由一个ICMP头(header)及ICMP数据(data)构成。
- ICMP头由类型(type),代码(code),校验(checksum),标识(identifier)以及编号(sequence numbers)构成。
- 类型代码为8表示这是一个echo_request。
- 编号的值(这里是256)被用来匹配与其对应的响应(echo responses)。



Echo response (回声响应)

下面第一幅图是Wireshark软件的截屏,第二幅图是其捕获到信息的文字内容。

ping tutorial scenario

No.
Time
  
Source   
Destination  Protocol length Info
2
0.028303
209.85.135.105 192.168.1.100 ICMP 74 Echo (ping) reply








Frame 2 (74 bytes on wire, 74 bytes captured)
Ethernet II, Src: ThomsonT_70:42:2d (00:18:f6:99:99:99), Dst: Dell_53:af:a6 (00:19:b9:11:11:11)
Internet Protocol, Src: 209.85.135.105 (209.85.135.105), Dst: 192.168.1.100 (192.168.1.100)

Version: 4

Header length: 20 bytes

Differentiated Services Field: 0x00 (DSCP 0x00: Default; ECN: 0x00)


0000
00..
= Differentiated Services Codepoint: Default (0x00)


....
..0.
= ECN-Capable Transport (ECT): 0


....
...0
= ECN-CE:  0

Total Length: 60

Identification: 0xf6bf (63167)

Flags: 0x00


 0..
= Reserved bit: Not Set


.0.
= Don't fragment: Not Set


..0
= More fragments: Not Set

Fragment offset: 0

Time to live: 251

Protocol: ICMP (0x01)

Header checksum: 0x7336 [correct]


[Good: True]


[Bad : False]

Source: 209.85.135.105 (209.85.135.105)

Destination: 192.168.1.100 (192.168.1.100)
Internet Control Message Protocol

Type: 0 (Echo (ping) reply)

Code: 0 ()

Checksum: 0x525c [correct]

Identifier: 0x0200

Sequence number: 256 (0x0100)

Data (32 bytes)








0000
61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70
abcdefghijklmnop
0010 71 72 73 74 75 76 77 61 62 63 64 65 66 67 68 69
qrstuvwabcdefghi



Data: 6162636465666768696A6B6C6D6E6F707172737475767761...



[Length: 32]

- Wireshark截图(第一幅图)中显示的是 不同OSI层 (Ethernet - IP - IMCP)中的信息。请参考章节"互联网控制协议单元(ICMP Packet)介绍"。
- 这条echo_response的TTL值为251,这个数字会被Ping显示给用户。
- 数据包在IP层被识别为ICMP。
- ICMP层由一个ICMP头(header)及ICMP数据(data)构成。
- ICMP头由类型(type),代码(code),校验(checksum),标识(identifier)以及编号(sequence numbers)构成。
- 类型代码为0表示这是一个echo_response。
- 编号的值(这里是256)被用来匹配与其对应的请求(echo request)。

返回页面顶端



PING的参数

Ping在Windows与Linux中的工作原理是完全一样的。不过在使用方法和输出信息格式上,两者有着细微的差别。

在默认情况下:

- Windows只发送4条ICMP请求,而Linux则会不断重复发送同样的消息,直到用户按下"Cltr + C"才会停止。
- ICMP数据区(data field)的大小:在Linux下为56 bytes,在Windows下为32 bytes。这也表明如果加上ICMP头的42 bytes,数据包的总长度在Linux下为98 bytes,在Windows下为74 bytes。

参数:

有一些参数的定义在Linux与Windows下不完全相同。比如-l选项在Windows下用于设定数据包的长度,而在Linux中,需要使用-s选项来完成这项工作。
下面的表格是Windows及Linux下Ping的详细参数列表。

输出结果:

在Linux的输出结果中,有一项特殊的值:平均偏差(mean deviation, mdev),它是基于响应时间(response time)计算得来的。这个值反映了响应时间的稳定性。用另一句话来讲,越小的平均偏差表示每一次Ping得到的响应时间越接近。

Windows下Ping的参数

c:/ping -h

Usage: ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS]
  [-r count] [-s count] [[-j host-list] | [-k host-list]]
[-w timeout] [-R] [-S srcaddr] [-4] [-6] target_name
Options:
-t  Ping the specified host until stopped.
To see statistics and continue - type Control-Break;
To stop - type Control-C.
-a     Resolve addresses to hostnames
-n count   Number of echo requests to send.
-l size  Send buffer size.
-f    Set Don't Fragment flag in packet (IPv4-only).
-i TTL Time To Live.
-v TOS Type Of Service (IPv4-only).
-r count  Record route for count hops (IPv4-only).
-s count Timestamp for count hops (IPv4-only).
-j host-list  Loose source route along host-list (IPv4-only).
-k host-list  Strict source route along host-list (IPv4-only).
-w timeout Timeout in milliseconds to wait for each reply.
-R Trace round-trip path (IPv6-only).
-S srcaddr  Source address to use (IPv6-only).
-4    Force using IPv4.
-6    Force using IPv6.

Ping arguments with Linux

#man ping

NAME


ping, ping6 - send ICMP ECHO_REQUEST to network hosts



SYNOPSIS


ping [-LRUbdfnqrvVaAB] [-c count] [-i interval] [-l preload] [-p pattern] [-s packetsize] [-t ttl] [-w deadline] [-F flowlabel] [-I interface] [-M hint] [-Q tos] [-S sndbuf] [-T timestamp option] [-W timeout] [hop ...] destination



DESCRIPTION


ping uses the ICMP protocolâs mandatory ECHO_REQUEST datagram to elicit an ICMP ECHO_RESPONSE from a host or gateway.  ECHO_REQUEST datagrams (ââpingsââ) have an  IP and ICMP header, followed by a struct timeval and then an arbitrary number of ââpadââ bytes used to fill out the packet



OPTIONS


-a Audible ping.




-A Adaptive ping. Interpacket interval adapts to round-trip time, so that effectively not more than one (or more, if preload is set) unanswered probes present in the network. Minimal interval is 200msec for not super-user.  On networks with low rtt this mode is essentially equivalent to flood mode.




-b Allow pinging a broadcast address.




-B Do not allow ping to  change  source  address  of  probes.   The address is bound to one selected when ping starts.




-c count


Stop after sending count ECHO_REQUEST packets. With deadline option, ping waits for count ECHO_REPLY packets, until the timeout expires.   




-d Set  the SO_DEBUG option on the socket being used.  Essentially, this socket option is not used by Linux kernel.   




-F flow label


Allocate and set 20 bit flow  label  on  echo  request  packets.  (Only  ping6).  If  value  is zero, kernel allocates random flow label.




-f Flood ping. For every ECHO_REQUEST sent a period ââ.ââ is printed, while for ever ECHO_REPLY received a backspace is printed.  This provides a  rapid  display of  how many packets are being dropped.  If interval is not given, it sets interval to zero and outputs packets as fast as they come back or one hundred times per second, whichever is more.  Only the super-user may use this option with zero interval.




 -i interval


Wait interval seconds between sending each packet.  The default is to wait for one second between each packet normally, or not to wait  in  flood  mode.  Only super-user may set interval to values less 0.2 seconds.




-I interface address


Set  source  address to specified interface address. Argument may be numeric IP address or name of device. When pinging IPv6 link-local address this option is required.




-l preload


If preload is specified, ping sends that many packets not waiting for reply.  Only the super-user may select preload more than 3.




-L


Suppress loopback of multicast packets.  This flag only applies if the ping destination is a multicast address.




-n


Numeric output only.  No attempt will be made to lookup symbolic names for host addresses.




-p pattern


You may specify up to 16 ââpadââ bytes to fill out the packet you send.  This is useful for diagnosing data-dependent problems in a network.  For example,  -p ff will cause the sent packet to be filled with all ones.




-Q tos


Set  Quality of Service -related bits in ICMP datagrams.  tos can be either decimal or hex number.  Traditionally (RFC1349), these have been interpreted as: 0 for reserved (currently being redefined as congestion control), 1-4 for Type of Service and 5-7 for Precedence.  Possible settings for Type  of  Service  are: minimal  cost: 0x02, reliability: 0x04, throughput: 0x08, low delay: 0x10.  Multiple TOS bits should not be set simultaneously.  Possible settings for special Precedence range from priority (0x20) to net control (0xe0).  You must be root (CAP_NET_ADMIN capability) to use Critical or  higher  precedence  value.   You cannot  set bit 0x01 (reserved) unless ECN has been enabled in the kernel.  In RFC2474, these fields has been redefined as 8-bit Differentiated Services (DS), consisting of: bits 0-1 of separate data (ECN will be used, here), and bits 2-7 of Differentiated Services Codepoint (DSCP).




-q


Quiet output.  Nothing is displayed except the summary lines at startup time and when finished.




-R


Record route.  Includes the RECORD_ROUTE option in the ECHO_REQUEST packet and displays the route buffer on returned packets.  Note that the IP header is only large enough for nine such routes.  Many hosts ignore or discard this option.




-r


Bypass  the  normal  routing  tables  and  send  directly  to a host on an attached interface.  If the host is not on a directly-attached network, an error is
 returned.  This option can be used to ping a local host through an interface that has no route through it provided the option -I is also used.




-s packetsize


Specifies the number of data bytes to be sent.  The default is 56, which translates into 64 ICMP data bytes when combined with the  8  bytes  of  ICMP  header data.




-S sndbuf


Set socket sndbuf. If not specified, it is selected to buffer not more than one packet.

-t ttl


Set the IP Time to Live.




 -T timestamp option


Set  special  IP  timestamp  options.  timestamp option may be either tsonly (only timestamps), tsandaddr (timestamps and addresses) or tsprespec host1 [host2[host3 [host4]]] (timestamp prespecified hops).




-M hint


Select Path MTU Discovery strategy.  hint may be either do (prohibit fragmentation, even local one), want (do PMTU discovery,  fragment  locally  when  packet size is large), or dont (do not set DF flag).




-U Print full user-to-user latency (the old behaviour). Normally ping prints network round trip time, which can be different f.e. due to DNS failures.




-v  Verbose output.




-V Show version and exit.




-w deadline


Specify a timeout, in seconds, before ping exits regardless of how many packets have been sent or received. In this case ping does not stop after count packet are sent, it waits either for deadline expire or until count probes are answered or for some error notification from network.




-W timeout


Time to wait for a response, in seconds. The option affects only timeout in absense of any responses, otherwise ping waits for two RTTs.

Top of the page



ICMP头(HEADER)

ICMP数据头由以下部分组成:

- 类型(Type)
- 代码(Code)
- 校验(Checksum)
- 标识(ID)
- 编号(Sequence)

下面是ICMP协议的结构。

ping icmp header

下面是ICMP的类型(TYPE)。

Type  Description
   
0 Echo reply
3 Destination unreachable
4 Source quench
5 Redirect
8 Echo request
9 Router advertisement
10 Router solicitation
11 Time exceeded
12 Parameter problem
13 Timestamp request
14 Timestamp reply
15 Information request
16 Information reply
17 Address mask request
18 Address mask reply
30 Traceroute
返回页面顶端



IP地址和主机名称(hostname)

Ping也经常被用于将IP地址转换为主机名称,或者反过来。

查询openmaniak网站的IP地址:

ping www.openmaniak.com

Pinging openmaniak.com [84.16.88.15] with 32 bytes of data:

Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 84.16.88.15:
      Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

主机没有回应,但这对于本例来说并不重要。我们已经在命令的返回结果中得到了我们关心的IP地址。

如果你想知道一个IP地址所对应的主机名称:

ping -a 84.16.88.15

Pinging imu138.infomaniak.ch [84.16.88.15] with 32 bytes of data:

Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 84.16.88.15:
      Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

对应IP地址84.16.88.15的主机名为imu138.infomaniak.ch。

返回页面顶端





If you liked our tutorials, don't hesitate to support us and visit our sponsors!
Si vous aimez nos tutoriaux, n'hésitez pas à nous supporter et visiter nos sponsors!