命令行大全

ping

瑞兹 发表于 2020-11-11 09:49浏览次数:

在类似Unix的操作系统上,ping命令将ICMP ECHO_REQUEST数据包发送到网络主机。

查看英文版

目录:

1 ping 运行系统环境

2 ping 描述

3 ping 语法

4 ping 笔记

5 ping 例子

ping 运行系统环境

Linux

ping 描述

ping是一种向网络上的另一台计算机发送网络数据并从其接收网络数据的简单方法。它通常用于最基本的级别上测试另一个系统是否可以通过网络访问,如果可以,则要花费多少时间来交换该数据。

所述ping效用使用ICMP协议的强制性ECHO_REQUEST数据报从引发ICMP ECHO_RESPONSE主机或网关。ECHO_REQUEST数据报(“ ping”)具有IP和ICMP标头,后跟一个struct timeval,然后是任意数量的用于填充数据包的“pad”字节。

ping is a simple way to send network data to, and receive network data from, another computer on a network. It is frequently used to test, at the most basic level, whether another system is reachable over a network, and if so, how much time it takes for that data to be exchanged.

The ping utility 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.

查看英文版

查看中文版

ping 语法

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

选件

-a 可听见的ping。
-A 自适应ping。数据包间的间隔适应于往返时间,因此有效地在网络中不超过一个(或多个,如果设置了预加载)未回答的探针。除超级用户外,任何其他用户的最小间隔为200毫秒。在具有低RTT(往返时间)的网络上,此模式实质上等效于泛洪模式(请参阅下面的-f)。
-b 允许ping通广播地址。
-B 不允许ping更改探针的源地址。ping开始时,该地址将绑定到所选的地址。
-m mark 使用标记标记出局的数据包。由于内核中的各种原因,例如使用策略路由选择特定的出站处理,这很有用。
-c count 发送计数 ECHO_REQUEST数据包后停止。使用deadline选项,ping将等待计数ECHO_REPLY数据包,直到超时到期。
-d 在正在使用的套接字上设置SO_DEBUG选项。Linux内核不使用此套接字选项。
-F 洪水坪。对于每个发送的ECHO_REQUEST,将打印一个句点(“  ”),而对于每个接收的ECHO_REPLY,则打印一个退格键。这样可以快速显示正在丢弃的数据包数量。如果未指定间隔(请参见下面的-i),则它将间隔设置为零,并以最快的速度输出数据包或每秒输出一百次,以较大者为准。仅超级用户可以以零间隔使用此选项。
-i interval 发送每个数据包之间的等待间隔秒数。默认设置是在每个数据包之间正常等待一秒钟,或者在泛洪模式下完全不等待(请参见上面的-f)。仅超级用户可以将间隔设置为小于0.2秒的值。
-I interface-address 将源地址设置为指定的接口地址。参数可以是数字IP地址或设备名称。ping IPv6链接本地地址时,此选项是必需的。
-l preload 如果指定了预加载,则ping发送不等待回复的许多数据包。只有超级用户可以选择大于3预载
-L 抑制多播数据包的环回。仅当ping目标是多播地址时,此标志才适用。
-N nioption 发送ICMPv6节点信息查询(RFC 4620),而不是回显请求。nioption可能是以下之一:

name 查询节点名称。
ipv6 IPv6地址查询。有几个特定于IPv6的标志:
ipv6-global 请求IPv6全局作用域地址。
ipv6-sitelocal 请求IPv6站点本地地址。
ipv6-linklocal 请求IPv6链接本地地址。
ipv6-all 在其他接口上请求IPv6地址。
ipv4 IPv4地址查询。有一个特定于IPv4的标志:
ipv4-all 在其他接口上请求IPv4地址。
subject-ipv6 = ipv6addr IPv6主题地址。
subject-ipv4 = ipv4addr IPv4主题地址。
subject-name =nodename 主题名称。如果包含多个点,则假定为完全限定的域名。
subject-fqdn =nodename 主题名称。始终采用完全限定的域名。
-n 仅数字输出。不会尝试查找主机地址的符号名称。
-p pattern 您最多可以指定16个“pad”字节来填写发送的数据包。这对于诊断网络中与数据相关的问题很有用。例如,-p FF将导致发送的分组来填充所有的:FF255中的十六进制,或11111111在二进制。
-D 在每行之前打印时间戳(unix时间+微秒)。
-Q tos 在ICMP数据报中设置与服务质量相关的位。tos可以是十进制或十六进制数。传统上(RFC1349),这些都被解释为:0为保留(当前正被重新定义为拥塞控制),1 - 4为TOS(服务类型)和5 - 7为优先。服务类型的可能设置是:最低成本:0x02,可靠性:0x04,吞吐量:0x08,低延迟:0x10。不应同时设置多个TOS位。特殊优先级的可能设置范围是从优先级(0x20)到网络控制(0xe0)。您必须是root用户(CAP_NET_ADMIN功能)才能使用Critical或更高的优先级值。除非在内核中启用了ECN,否则无法将位0x01(保留)置1 。在RFC2474中,这些字段被重新定义为8位区分服务(DS),其中包括:位0 - 1单独的数据的(ECN将被使用,在这里),和位2 - 7差分服务代码点(DSCP)的。
-q 安静的输出。除了启动时和完成时的摘要行以外,什么都没有显示。
-R 记录路线。在ECHO_REQUEST数据包中包括RECORD_ROUTE选项,并在返回的数据包上显示路由缓冲区。请注意,IP标头仅足以容纳九条此类路由。许多主机忽略或放弃此选项。
-r 绕过常规路由表,并直接通过连接的接口发送到主机。如果主机不在直接连接的网络上,则返回错误。如果还使用了选项-I(请参见上文),则该选项可用于通过没有路由通过它的接口ping本地主机。
-s PACKETSIZE 指定要发送的数据字节数。缺省值为56,当与8个字节的ICMP头数据结合使用时,它将转换为64个ICMP数据字节。
-S sndbuf 设置套接字sndbuf。如果未指定,则选择它最多缓冲一个数据包。
-t ttl 设置IP TTL(生存时间)。
-T timestamp-option 设置特殊的IP时间戳选项。时间戳选项可以是tsonly(仅时间戳),tsandaddr(时间戳和地址)或tsprespec host1 [ host2 [ host3 [ host4 ]]]](时间戳预先指定的跃点)。
-M hint 选择路径MTU发现策略。提示可以是do(禁止分裂,甚至当地的一个),want(做PMTU发现,本地片段时,报文尺寸较大),或dont(不设置DF标志)。
-U 打印完整的用户到用户延迟(旧版ping行为)。通常ping命令会打印网络往返时间,由于DNS故障,时间可能有所不同。
-v 详细输出。
-V 显示版本号,然后退出。
-w deadline 指定ping退出之前的超时(以秒为单位),无论发送或接收了多少个数据包。在这种情况下,在发送计数数据包后ping不会停止,它会等待截止日期到期或直到响应计数探测或网络中的某些错误通知为止。
-W timeout 等待响应的时间(以秒为单位)。该选项仅在没有任何响应的情况下影响超时,否则ping将等待两个RTT。
ping [-LRUbdfnqrvVaAB] [-c count] [-m mark] [-i interval] [-l preload] 
     [-p pattern] [-s packetsize] [-t ttl] [-w deadline] [-F flowlabel] 
     [-I interface] [-M hint] [-N nioption] [-Q tos] [-S sndbuf] 
     [-T timestamp option] [-W timeout] [hop ...] destination

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 are present in the network. Minimal interval is 200msec for any user other than the super-user. On networks with low RTT (round trip time), this mode is essentially equivalent to flood mode (see -f, below).
-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.
-m mark use mark to tag the packets going out. This is useful for variety of reasons in the kernel such as using policy routing to select specific outbound processing.
-c count Stop after sending count ECHO_REQUEST packets. With the deadline option, ping waits for count ECHO_REPLY packets, until the timeout expires.
-d Set the SO_DEBUG option on the socket being used. This socket option is not used by Linux kernel.
-f Flood ping. For every ECHO_REQUEST sent, a period (".") is printed, while for every ECHO_REPLY received, a backspace is printed. This provides a rapid display of how many packets are being dropped. If an interval is not given (see -i, below), 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 an interval of zero.
-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 at all in flood mode (see -f, above). 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 a numeric IP address or the name of the device. When pinging IPv6 link-local addresses, 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 a preload of more than 3.
-L Suppress loopback of multicast packets. This flag only applies if the ping destination is a multicast address.
-N nioption Send ICMPv6 Node Information Queries (RFC 4620), instead of Echo Requests. nioption may be one of the following:

name Queries for Node Names.
ipv6 Queries for IPv6 Addresses. There are several IPv6 specific flags:
ipv6-global Request IPv6 global-scope addresses.
ipv6-sitelocal Request IPv6 site-local addresses.
ipv6-linklocal Request IPv6 link-local addresses.
ipv6-all Request IPv6 addresses on other interfaces.
ipv4 Queries for IPv4 addresses. There is one IPv4 specific flag:
ipv4-all Request IPv4 addresses on other interfaces.
subject-ipv6=ipv6addr IPv6 subject address.
subject-ipv4=ipv4addr IPv4 subject address.
subject-name=nodename Subject name. If it contains more than one dot, fully-qualified domain name is assumed.
subject-fqdn=nodename Subject name. Fully-qualified domain name is always assumed.
-n Numeric output only. No attempt will be made to look up 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: ff is 255 in hexadecimal, or 11111111 in binary.
-D Print timestamp (unix time + microseconds) before each line.
-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 TOS (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 is enabled in the kernel. In RFC2474, these fields are 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 (see above).
-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 TTL (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 (legacy ping behavior). Normally ping prints network round trip time, which can be different f.e. due to DNS failures.
-v Verbose output.
-V Display version number, 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 to 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 absence of any responses, otherwise ping waits for two RTTs.

查看英文版

查看中文版

ping 笔记

使用ping进行故障隔离时,应首先在localhost上运行它,以验证本地网络接口是否已启动并正在运行。然后,应该“ ping”越来越远的主机和网关。计算往返时间和丢包统计信息。如果接收到重复的数据包,尽管这些数据包的往返时间用于计算最小/平均/最大往返时间,但它们仍不包含在丢包计算中。当指定数量的数据包已发送(或接收)或程序以SIGINT 信号终止时,将显示简短摘要。在不终止信号SIGQUIT的情况下,可以获得更短的电流统计信息。

如果ping根本没有收到任何答复数据包,它将以代码1退出。如果同时指定了数据包计数和截止期限,并且在截止期限到来时接收到的数据包少于计数,则它也将以代码1退出。出现其他错误时,代码2退出。否则,它以代码0退出。这样就可以使用退出代码查看主机是否处于活动状态。

ping适用于网络测试,测量和管理。由于它可能会施加在网络上,因此在正常操作或自动脚本中使用ping是不明智的。

ICMP数据包详细信息

不带选项的IP标头为20个字节。ICMP ECHO_REQUEST数据包包含额外的8个字节的ICMP标头,后跟任意数量的数据。给定数据包大小时,这表示该额外数据的大小(默认值为56)。因此,在类型为ICMP ECHO_REPLY的IP数据包中接收到的数据量将始终比请求的数据空间(ICMP标头)多8个字节。

如果数据空间至少为struct timeval的大小,则ping使用该空间的开始字节来包括时间戳,该时间戳将用于往返时间的计算。如果数据空间较短,则不会给出往返时间。

重复和损坏的数据包

ping将报告重复和损坏的数据包。重复的数据包永远不会发生,并且似乎是由不适当的链路级重传引起的。在许多情况下可能会发生重复,并且很少(如果有的话)是一个好兆头,尽管重复水平较低的情况可能并不总是会引起警报。

损坏的数据包是引起警报的严重原因,并且通常表明ping数据包路径中某处(网络或主机中)的硬件损坏。

尝试不同的数据模式

(网络间)层决不应根据数据部分中包含的数据对数据包进行不同的处理。不幸的是,已知与数据有关的问题会潜入网络,并且很长一段时间都不会被发现。在许多情况下,有问题的特定模式是没有足够“过渡”的东西,例如全1或全零,或者在边缘的模式(例如几乎全零)。在命令行上指定全零的数据模式(例如)并不一定足够,因为感兴趣的模式位于数据链接级别,并且您键入的内容与控制器传输的内容之间的关系可以是复杂。

这意味着,如果您遇到与数据相关的问题,则可能必须进行大量测试才能找到它。如果幸运的话,您可能会设法找到一个文件,该文件要么不能通过网络发送,要么需要比其他类似长度的文件传输更长的时间。然后,您可以检查该文件中是否存在可以使用-p选项进行测试的重复模式。

TTL详细信息

IP数据包的TTL(生存时间)值表示该数据包在丢弃之前可以通过的IP路由器的最大数量。实际上,您可以期望Internet中的每个路由器将TTL字段减一。

该TCP / IP规范规定为TCP数据包的TTL字段应该设置为60,但许多系统使用较小的值(4.3 BSD用途30,4.2使用15)。

该字段的最大可能值为255,并且大多数Unix系统将ICMP ECHO_REQUEST数据包的TTL字段设置为255。这就是为什么您会发现可以ping某些主机,但不能通过telnet或ftp到达它们的原因。

在正常操作中,ping从收到的数据包中打印ttl值。远程系统收到ping数据包时,可以在响应中使用TTL字段执行以下三项操作之一:

  • 不改变它;这是Berkeley Unix系统在4.3 BSD Tahoe发行之前所做的。在这种情况下,收到的数据包中的TTL值将为255减去往返路径中路由器的数量。 
  • 设置为255;这就是当前的Berkeley Unix系统所做的。在这种情况下,收到的数据包中的TTL值为255减去从远程系统到ping主机的路径中的路由器数量。 
  • 将其设置为其他值。一些机器对ICMP数据包使用的值与用于TCP数据包的值相同,例如30或60。其他机器可能完全使用通配值。

补充说明

  • 许多主机和网关会忽略RECORD_ROUTE选项。 
  • IP标头的最大长度太小,以至于RECORD_ROUTE之类的选项无法完全发挥作用,尽管对此无法做很多事情。 
  • 通常不建议进行泛洪ping操作,并且仅应在非常受控的条件下对广播地址进行泛洪ping操作。 

When using ping for fault isolation, it should first be run on the localhost, to verify that the local network interface is up and running. Then, hosts and gateways further and further away should be "pinged". Round-trip times and packet loss statistics are computed. If duplicate packets are received, they are not included in the packet loss calculation, although the round trip time of these packets is used in calculating the minimum/average/maximum round-trip time numbers. When the specified number of packets have been sent (and received) or if the program is terminated with a SIGINT signal, a brief summary is displayed. Shorter current statistics can be obtained without termination of process with signal SIGQUIT.

If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise, it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.

ping is intended for use in network testing, measurement and management. Because of the load it can impose on the network, it is unwise to use ping during normal operations or from automated scripts.

ICMP packet details

An IP header without options is 20 bytes. An ICMP ECHO_REQUEST packet contains an additional 8 bytes worth of ICMP header followed by an arbitrary amount of data. When a packetsize is given, this indicated the size of this extra piece of data (the default is 56). Thus the amount of data received inside of an IP packet of type ICMP ECHO_REPLY will always be 8 bytes more than the requested data space (the ICMP header).

If the data space is at least of size of struct timeval, ping uses the beginning bytes of this space to include a timestamp which it uses in the computation of round trip times. If the data space is shorter, no round trip times are given.

Duplicate and damaged packets

ping will report duplicate and damaged packets. Duplicate packets should never occur, and seem to be caused by inappropriate link-level retransmissions. Duplicates may occur in many situations and are rarely (if ever) a good sign, although the presence of low levels of duplicates may not always be cause for alarm.

Damaged packets are serious cause for alarm and often indicate broken hardware somewhere in the ping packet's path (in the network or in the hosts).

Trying different data patterns

The (inter)network layer should never treat packets differently depending on the data contained in the data portion. Unfortunately, data-dependent problems have been known to sneak into networks and remain undetected for long periods of time. In many cases, the particular pattern that has problems is something that doesn't have sufficient "transitions," such as all ones or all zeros, or a pattern right at the edge, such as almost all zeros. It isn't necessarily enough to specify a data pattern of all zeros (for example) on the command line because the pattern that is of interest is at the data link level, and the relationship between what you type and what the controllers transmit can be complicated.

This means that if you have a data-dependent problem you will probably have to do a lot of testing to find it. If you are lucky, you may manage to find a file that either can't be sent across your network or that takes much longer to transfer than other similar length files. You can then examine this file for repeated patterns that you can test using the -p option.

TTL details

The TTL (time-to-live) value of an IP packet represents the maximum number of IP routers that the packet can go through before being thrown away. In practice, you can expect each router in the Internet to decrement the TTL field by exactly one.

The TCP/IP specification states that the TTL field for TCP packets should be set to 60, but many systems use smaller values (4.3 BSD uses 30, 4.2 used 15).

The maximum possible value of this field is 255, and most Unix systems set the TTL field of ICMP ECHO_REQUEST packets to 255. This is why you will find you can ping some hosts, but not reach them with telnet or ftp.

In normal operation, ping prints the ttl value from the packet it receives. When a remote system receives a ping packet, it can do one of three things with the TTL field in its response:

  • Not change it; this is what Berkeley Unix systems did before the 4.3 BSD Tahoe release. In this case, the TTL value in the received packet will be 255 minus the number of routers in the round-trip path.
  • Set it to 255; this is what current Berkeley Unix systems do. In this case, the TTL value in the received packet will be 255 minus the number of routers in the path from the remote system to the pinging host. 
  • Set it to some other value. Some machines use the same value for ICMP packets that they use for TCP packets, for example either 30 or 60. Others may use completely wild values.

Additional notes

  • Many hosts and gateways ignore the RECORD_ROUTE option. 
  • The maximum IP header length is too small for options like RECORD_ROUTE to be completely useful, although there's not much that can be done about this. 
  • Flood pinging is not recommended in general, and flood pinging the broadcast address should only be done under very controlled conditions. 

查看英文版

查看中文版

ping 例子

ping google.com

对主机google.com进行Ping操作,看看它是否还活着。

ping google.com -c 1

对主机google.com进行一次Ping操作。输出将类似于以下内容:

PING google.com (204.228.150.3) 56(84) bytes of data.
64 bytes from www.google.com (204.228.150.3): icmp_seq=1 ttl=63 time=0.267 ms
--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.267/0.267/0.267/0.000 ms
ping google.com

Ping the host google.com to see if it is alive.

ping google.com -c 1

Ping the host google.com once. Output will resemble the following:

PING google.com (204.228.150.3) 56(84) bytes of data.
64 bytes from www.google.com (204.228.150.3): icmp_seq=1 ttl=63 time=0.267 ms
--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.267/0.267/0.267/0.000 ms

查看英文版

查看中文版