0%

网络中不中,先看ping行不行

网络中不中,先看ping行不行

linux系统里面如果想判断网络的好坏,脑海中蹦出的第一个命令就是ping了。

官方定义为:

ping - send ICMP ECHO_REQUEST to network hosts

ping命令基本是最常用的网络命令,它可以用来测试与目标主机的连通性。

ping使用ICMP传输协议,通过发送ICMP ECHO_REQUEST数据包到网络主机,并显示返回的相应情况,根据这些信息就可以判断目标主机是否可以访问,在发送的过程中还会有一个时间戳用来计算网络的状态。

不过有些服务器为了防止通过ping探测到,可能会在防火墙或者内核参数中禁止ping命令,这样的话,可能虽然目标主机可以访问,但是无法ping通,所以并不能说ping不通的网络就是不能访问的。

需要注意linux下的ping和windows下的ping稍有区别,linux下ping不会自动终止,需要按ctrl+c终止或者用参数-c指定要求完成的回应次数。

语法

ping的使用说实话挺复杂,挺多的,不过常用的这篇短文基本就够了。

详细如下:

1
2
3
4
5
# ALL
$ ping [-aAbBdDfhLnOqrRUvV46] [-c count] [-F flowlabel] [-i interval] [-I interface] [-l preload] [-m mark] [-M pmtudisc_option] [-N node‐info_option] [-w deadline] [-W timeout] [-p pattern] [-Q tos] [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp option] [hop ...] destination

# 较常用的选项如下:
$ ping [-c count] [-i interval] destination

参数说明

  • -c <完成次数> 设置完成要求回应的次数。

  • -i interval 指定收发信息的间隔时间。

不加任何参数

如果不加任何参数,查看是否ping

1
2
3
4
5
6
7
8
9
10
11
12
$ ping www.baidu.com   
PING www.a.shifen.com (115.239.210.27) 56(84) bytes of data.
64 bytes from 115.239.210.27: icmp_seq=1 ttl=52 time=6.06 ms
64 bytes from 115.239.210.27: icmp_seq=2 ttl=52 time=5.56 ms
64 bytes from 115.239.210.27: icmp_seq=3 ttl=52 time=5.67 ms
64 bytes from 115.239.210.27: icmp_seq=4 ttl=52 time=5.82 ms
64 bytes from 115.239.210.27: icmp_seq=5 ttl=52 time=5.70 ms
64 bytes from 115.239.210.27: icmp_seq=6 ttl=52 time=5.79 ms
^C # 此处输入了Ctrl+C强制退出
--- 192.168.1.123 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 3999ms
rtt min/avg/max/mdev = 0.152/0.159/0.172/0.017 ms

可以看到可以pingwww.baidu.com,时延还算比较OK,几个毫秒量级。

这里看一下几个字段的含义,其中:

56(84) bytes of data:表示默认的数据包长度为56字节;

time=5.56ms:表示响应的时间,值越小,证明连接越快;

TTL=52:TTL是Time To Live的缩写,表示DNS记录在DNS服务器上存在的时间,是IP协议包的一个值,告诉路由器啥时候抛弃这个数据包,(大体上可以通过这个值来判断目标类型的操作系统。)

发送指定数目

可以通过 参数-c 来发送指定数目的包后停止

1
2
3
4
5
6
7
8
9
10
11
$ ping www.baidu.com -c 5
PING www.a.shifen.com (115.239.211.112) 56(84) bytes of data.
64 bytes from 115.239.211.112: icmp_seq=1 ttl=52 time=6.03 ms
64 bytes from 115.239.211.112: icmp_seq=2 ttl=52 time=5.96 ms
64 bytes from 115.239.211.112: icmp_seq=3 ttl=52 time=5.79 ms
64 bytes from 115.239.211.112: icmp_seq=4 ttl=52 time=5.79 ms
64 bytes from 115.239.211.112: icmp_seq=5 ttl=52 time=6.21 ms

--- www.a.shifen.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4007ms
rtt min/avg/max/mdev = 5.791/5.958/6.215/0.186 ms

此时将在发送5次数据包以后自动停止,在Linux里面,如果不加这个参数,是会一直发送运行的。

设定发送时间间隔

可以通过 参数 -i N指定每个N秒发送一次信息,如下将每隔3秒发送一次ping信息。

1
2
3
4
5
6
7
8
9
10
11
12
$ ping www.baidu.com -i 3
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=55 time=28.6 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=55 time=28.6 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=3 ttl=55 time=28.6 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=4 ttl=55 time=28.6 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=5 ttl=55 time=28.6 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=6 ttl=55 time=28.6 ms
^C
--- www.a.shifen.com ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 15041ms
rtt min/avg/max/mdev = 28.650/28.670/28.697/0.139 ms

如上,每隔3秒会发送一次,对于需要持续检测或者记录的可以考虑适当加大这个时间间隔。

注意,只有管理员可以设置小于0.2秒的时间间隔。所以这个数值可以是浮点数~

组合使用

上面的几个例子是可以配合使用的,比如

1
2
3
4
5
6
7
8
9
10
11
$ ping www.baidu.com -c 4 -i 5 
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=55 time=29.4 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=55 time=29.3 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=55 time=29.4 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=4 ttl=55 time=29.4 ms

--- www.a.shifen.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 20045ms
rtt min/avg/max/mdev = 29.396/29.428/29.461/0.110 ms

这个例子为:每个5秒查询一次,一共查询4次,然后退出。

处无为之事,行不言之教;作而弗始,生而弗有,为而弗恃,功成不居!

欢迎关注我的其它发布渠道