目录
Linux ping 命令
Linux ping 命令用于检测与另一个主机之间的网络连接。
执行 ping 指令会使用 ICMP(Internet Control Message Protocol) 传输协议,发出要求回应的信息。若远端主机的网络功能没有问题,就会回应该信息,因而得知该主机运作正常。
语法
ping [-dfnqrRv][-c<完成次数>][-i<间隔秒数>][-I<网络界面>][-l<前置载入>][-p<范本样式>][-s<数据包大小>][-t<存活数值>][主机名称或IP地址]
参数说明:
- -d 使用Socket的SO_DEBUG功能。
- -c <完成次数> 设置完成要求回应的次数。
- -f 极限检测。
- -i<间隔秒数> 指定收发信息的间隔时间。
- -I<网络界面> 使用指定的网络接口送出数据包。
- -l<前置载入> 设置在送出要求信息之前,先行发出的数据包。
- -n 只输出数值。
- -p<范本样式> 设置填满数据包的范本样式。
- -q 不显示指令执行过程,开头和结尾的相关信息除外。
- -r 忽略普通的Routing Table,直接将数据包送到远端主机上。
- -R 记录路由过程。
- -s<数据包大小> 设置数据包的大小。
- -t<存活数值> 设置存活数值TTL的大小。
- -v 详细显示指令的执行过程。
- -w <deadline> 在 deadline 毫秒后退出。
- -W <timeout> 在等待 timeout 毫秒后开始执行。
实例
1 检测是否与主机连通
ping www.baidu.com //ping主机
AAA@master:~$ ping www.baidu.com
PING www.baidu.com(2408:871a:2100:2:0:ff:b09f:237 (2408:871a:2100:2:0:ff:b09f:237)) 56 data bytes
64 bytes from 2408:871a:2100:2:0:ff:b09f:237 (2408:871a:2100:2:0:ff:b09f:237): icmp_seq=1 ttl=48 time=39.4 ms
64 bytes from 2408:871a:2100:2:0:ff:b09f:237 (2408:871a:2100:2:0:ff:b09f:237): icmp_seq=2 ttl=48 time=39.4 ms
64 bytes from 2408:871a:2100:2:0:ff:b09f:237 (2408:871a:2100:2:0:ff:b09f:237): icmp_seq=3 ttl=48 time=39.5 ms
64 bytes from 2408:871a:2100:2:0:ff:b09f:237 (2408:871a:2100:2:0:ff:b09f:237): icmp_seq=4 ttl=48 time=39.4 ms
64 bytes from 2408:871a:2100:2:0:ff:b09f:237 (2408:871a:2100:2:0:ff:b09f:237): icmp_seq=5 ttl=48 time=39.5 ms
64 bytes from 2408:871a:2100:2:0:ff:b09f:237 (2408:871a:2100:2:0:ff:b09f:237): icmp_seq=6 ttl=48 time=39.3 ms
64 bytes from 2408:871a:2100:2:0:ff:b09f:237 (2408:871a:2100:2:0:ff:b09f:237): icmp_seq=7 ttl=48 time=39.3 ms
64 bytes from 2408:871a:2100:2:0:ff:b09f:237 (2408:871a:2100:2:0:ff:b09f:237): icmp_seq=8 ttl=48 time=39.3 ms
64 bytes from 2408:871a:2100:2:0:ff:b09f:237 (2408:871a:2100:2:0:ff:b09f:237): icmp_seq=9 ttl=48 time=39.4 ms
64 bytes from 2408:871a:2100:2:0:ff:b09f:237 (2408:871a:2100:2:0:ff:b09f:237): icmp_seq=10 ttl=48 time=39.4 ms
64 bytes from 2408:871a:2100:2:0:ff:b09f:237 (2408:871a:2100:2:0:ff:b09f:237): icmp_seq=11 ttl=48 time=39.2 ms
^C
--- www.baidu.com ping statistics ---
11 packets transmitted, 11 received, 0% packet loss, time 10016ms
rtt min/avg/max/mdev = 39.298/39.429/39.532/0.073 ms
^C //需要手动终止Ctrl+C
2 指定接收包的次数
AAA@master:~$ ping -c 2 www.baidu.com
PING www.baidu.com(2408:871a:2100:2:0:ff:b09f:237 (2408:871a:2100:2:0:ff:b09f:237)) 56 data bytes
64 bytes from 2408:871a:2100:2:0:ff:b09f:237 (2408:871a:2100:2:0:ff:b09f:237): icmp_seq=1 ttl=48 time=39.5 ms
64 bytes from 2408:871a:2100:2:0:ff:b09f:237 (2408:871a:2100:2:0:ff:b09f:237): icmp_seq=2 ttl=48 time=39.2 ms
--- www.baidu.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 39.262/39.384/39.506/0.122 ms
//收到两次包后,自动退出