什么是 ping?

Ping(全称 Packet InterNet Groper) 是一种用来测试数据包是否能通过 IP 协议到达特定主机的管理员工具。

Ping 程序通过向目标主机发送一个 ICMP 请求,并等待其回应数据包。 它根据时间和成功次数来计算数据丢包率以及数据往返时间(Rouad-trip time, RTT)。

Unix/Linux 下的 ping 命令参数

$ ping -help
Usage: ping [-LRUbdfnqrvVaAD] [-c count] [-i interval] [-w deadline]
            [-p pattern] [-s packetsize] [-t ttl] [-I interface]
            [-M pmtudisc-hint] [-m mark] [-S sndbuf]
            [-T tstamp-options] [-Q tos] [hop1 ...] destination

目前许多操作系统也提供了用来测试 IPv6 网络的 ping6 工具。

$ ping6 -help
Usage: ping6 [-LUdfnqrvVaAD] [-c count] [-i interval] [-w deadline]
             [-p pattern] [-s packetsize] [-t ttl] [-I interface]
             [-M pmtudisc-hint] [-S sndbuf] [-F flowlabel] [-Q tclass]
             [[-N nodeinfo-option] ...]
             [hop1 ...] destination

Windows 下的 ping 命令参数

C:\>ping /?

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. This setting has been deprecated
                   and has no effect on the type of service field in the IP Header).
    -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             Use routing header to test reverse route also (IPv6-only).
    -S srcaddr     Source address to use.
    -4             Force using IPv4.
    -6             Force using IPv6.

Ping 程序测试示例

在 Linux 系统下,运行 ping 程序,向 www.yahoo.com 发送 5 个测试包的输出结果:

$ ping -c 5 www.yahoo.com
PING www.yahoo.com (116.214.12.74) 56(84) bytes of data.
64 bytes from 116.214.12.74: icmp_req=1 ttl=47 time=22.8 ms
64 bytes from 116.214.12.74: icmp_req=2 ttl=47 time=22.3 ms
64 bytes from 116.214.12.74: icmp_req=3 ttl=47 time=22.8 ms
64 bytes from 116.214.12.74: icmp_req=4 ttl=47 time=22.2 ms
64 bytes from 116.214.12.74: icmp_req=5 ttl=47 time=22.2 ms

--- www.yahoo.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4005ms
rtt min/avg/max/mdev = 22.228/22.503/22.853/0.316 ms

程序在完成 ping 检测之后,输出了详细的结果信息。数据往返时间(RTT)为 22.228 ms, 平均时间为 22.503 ms, 最长时间为 22.853 ms,其中测量的误差为 0.316 ms。

其他与 ping 相关的资源