首页 » ping - 网络是否可达

Linux下的ping命令

-i可设置ping的时间间隔。以下每隔6秒ping一次Google:

$ ping google.com -i6

如果不是管理员且把-i设置为低于0.2秒的时间间隔就会被系统禁止,被认为是泛洪攻击(Flood)了:

$ ping 127.0.0.1 -i 0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
ping: cannot flood; minimal interval, allowed for user, is 200ms

当然你可以切换到root去这么干:

# ping 127.0.0.1 -i 0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.008 ms

并且可以公开声明我就是要这么干(执行泛洪攻击):

# ping 127.0.0.1 -f
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
...^C
--- 127.0.0.1 ping statistics ---
88347 packets transmitted, 87885 received, 0% packet loss, time 20694ms
rtt min/avg/max/mdev = 0.000/0.004/1.975/0.040 ms, pipe 2, ipg/ewma 0.234/0.006 ms

有3种方法ping本地(Localhost):

$ ping 0
$ ping localhost
$ ping 127.0.0.1

-c指定ping的次数(在Linux下默认是一直ping下去不停止;Windows默认ping 5次):

$ ping google.com -n -c3
PING google.com (173.194.72.138) 56(84) bytes of data.
64 bytes from 173.194.72.138: icmp_seq=1 ttl=37 time=73.5 ms
64 bytes from 173.194.72.138: icmp_seq=2 ttl=37 time=76.6 ms
64 bytes from 173.194.72.138: icmp_seq=3 ttl=37 time=79.6 ms

--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 73.550/76.624/79.659/2.504 ms

-w指定ping的时间,超时后ping就会退出(默认1秒ping一次,设置3秒超时的话通常就发3个ping报文)。

$ ping baidu.com -w3
PING baidu.com (123.125.114.144) 56(84) bytes of data.
64 bytes from 123.125.114.144: icmp_seq=1 ttl=46 time=19.2 ms
64 bytes from 123.125.114.144: icmp_seq=2 ttl=46 time=18.0 ms
64 bytes from 123.125.114.144: icmp_seq=3 ttl=46 time=88.8 ms

--- baidu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 18.090/42.070/88.868/33.094 ms

-q抑制ping的输出,这样就可以获得一份统计数据而不必关系每个ping包的结果了:

$ ping google.com -c100 -i0.2 -q
PING google.com (74.125.31.138) 56(84) bytes of data.

--- google.com ping statistics ---
100 packets transmitted, 99 received, 1% packet loss, time 19855ms
rtt min/avg/max/mdev = 44.869/90.113/165.976/17.336 ms

也可以通过输入Ctrl+|随时获取一份统计信息(发送了一个SIGQUIT信号):

$ ping baidu.com 
PING baidu.com (220.181.111.86) 56(84) bytes of data.
64 bytes from 220.181.111.86: icmp_seq=1 ttl=53 time=2.78 ms
...
64 bytes from 220.181.111.86: icmp_seq=5 ttl=53 time=5.86 ms
5/5 packets, 0% loss, min/avg/ewma/max = 2.789/3.956/3.431/5.866 ms
64 bytes from 220.181.111.86: icmp_seq=6 ttl=53 time=5.59 ms
...
64 bytes from 220.181.111.86: icmp_seq=10 ttl=53 time=8.90 ms
11/10 packets, 9% loss, min/avg/ewma/max = 2.789/4.823/4.638/8.903 ms
64 bytes from 220.181.111.86: icmp_seq=11 ttl=53 time=21.8 ms
...

-s设置每个ping报文(其实是ICMP报文)的大小,默认是56:

$ ping google.com -s1024 -n     # ICMP回复报文被截断了
PING google.com (173.194.72.100) 1024(1052) bytes of data.
72 bytes from 173.194.72.100: icmp_seq=1 ttl=34 (truncated)
$ ping baidu.com -s1024 -n      # Baidu回复与Google不同
PING baidu.com (220.181.111.86) 1024(1052) bytes of data.
1032 bytes from 220.181.111.86: icmp_seq=1 ttl=53 time=7.11 ms
$ ping yahoo.com -s1024 -n      # 雅虎没响应;正常ping没问题
PING yahoo.com (98.139.183.24) 1024(1052) bytes of data.

分享

0