ping命令

ping命令

  ping是Packet InterNet Groper的缩写,即包网络探测。
  ping命令用来检测网络上两个主机的连通性,如果连通还可以报告网络延时。
  ping命令使用ICMP协议,在本机构建回声请求报文(Type为8)并发送给对方,根据是否接收到回声应答报文(Type为0)判断两个主机是否连通。
  可以直接ping对方的IP地址来检测连通性,例如:

[root@localhost ~]# ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=128 time=2.16 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=128 time=1.05 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=128 time=0.722 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=128 time=1.63 ms
64 bytes from 192.168.1.1: icmp_seq=5 ttl=128 time=0.660 ms
^C
--- 192.168.1.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4004ms
rtt min/avg/max/mdev = 0.660/1.248/2.167/0.576 ms
[root@localhost ~]#

  也可以ping对方的主机名或域名,例如:

[root@localhost ~]# ping www.kernel.org
PING sin.git.kernel.org (147.75.46.191) 56(84) bytes of data.
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=1 ttl=49 time=295 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=2 ttl=49 time=292 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=3 ttl=49 time=296 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=4 ttl=49 time=300 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=5 ttl=49 time=299 ms
^C
--- sin.git.kernel.org ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4005ms
rtt min/avg/max/mdev = 292.650/296.947/300.313/2.854 ms
[root@localhost ~]#

  ping命令默认会一直发送ping请求报文,直到用户通过Ctrl+C终止(或者通过kill发送信号终止)。可以使用选项-c指定发送ping请求报文的个数,例如:

[root@localhost ~]# ping -c 4 www.kernel.org
PING sin.git.kernel.org (147.75.46.191) 56(84) bytes of data.
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=1 ttl=49 time=296 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=2 ttl=49 time=291 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=3 ttl=49 time=295 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=4 ttl=49 time=288 ms

--- sin.git.kernel.org ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3001ms
rtt min/avg/max/mdev = 288.197/292.775/296.096/3.100 ms
[root@localhost ~]#

  上面的命令指定发送4个ping请求报文后,尝试接收ping应答报文,然后自动退出ping命令。
  也可以使用选项-w指定执行若干秒后自动退出ping命令,例如下面的例子10秒后自动退出ping命令:

[root@localhost ~]# ping -w 10 www.kernel.org
PING sin.git.kernel.org (147.75.46.191) 56(84) bytes of data.
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=1 ttl=49 time=296 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=2 ttl=49 time=302 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=3 ttl=49 time=289 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=4 ttl=49 time=291 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=5 ttl=49 time=297 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=6 ttl=49 time=295 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=7 ttl=49 time=288 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=8 ttl=49 time=296 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=9 ttl=49 time=288 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=10 ttl=49 time=294 ms

--- sin.git.kernel.org ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9014ms
rtt min/avg/max/mdev = 288.671/294.177/302.823/4.281 ms
[root@localhost ~]#

  ping命令默认每隔1秒发送一个ping请求报文,可以通过选项-i指定发送的时间间隔,时间单位是秒。例如:

[root@localhost ~]# ping -i 3 www.kernel.org
PING sin.git.kernel.org (147.75.46.191) 56(84) bytes of data.
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=1 ttl=49 time=289 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=2 ttl=49 time=287 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=3 ttl=49 time=284 ms
^C
--- sin.git.kernel.org ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 6005ms
rtt min/avg/max/mdev = 284.812/287.206/289.406/1.880 ms
[root@localhost ~]#

  间隔时间可以是一位小数,例如:

[root@localhost ~]# ping -i 0.5 www.kernel.org
PING sin.git.kernel.org (147.75.46.191) 56(84) bytes of data.
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=1 ttl=49 time=292 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=2 ttl=49 time=301 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=3 ttl=49 time=293 ms
64 bytes from 147.75.46.191 (147.75.46.191): icmp_seq=4 ttl=49 time=300 ms
^C
--- sin.git.kernel.org ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 1501ms
rtt min/avg/max/mdev = 292.315/296.861/301.341/4.154 ms
[root@localhost ~]#