PING程序是我们使用的比较多的用于测试网络连通性的程序。PING程序基于ICMP,使用ICMP的回送请求和回送应答来工作。由计算机网络课程知道,ICMP是基于IP的一个协议,ICMP包通过IP的封装之后传递。
它的作用主要为:
1、用来检测网络的连通情况和分析网络速度
2、根据域名得到服务器IP
3、根据ping返回的TTL值来判断对方所使用的操作系统及数据包经过路由器数量。
[root@localhost ~]# ping
Usage: ping [-aAbBdDfhLnOqrRUvV64] [-c count] [-i interval] [-I interface]
[-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
[-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
[-w deadline] [-W timeout] [hop1 …] destination
Usage: ping -6 [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
[-l preload] [-m mark] [-M pmtudisc_option]
[-N nodeinfo_option] [-p pattern] [-Q tclass] [-s packetsize]
[-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline]
[-W timeout] destination
[root@localhost ~]# ping 172.17.0.3
PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.033 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.035 ms
64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.040 ms
64 bytes from 172.17.0.3: icmp_seq=4 ttl=64 time=0.039 ms
^C
— 172.17.0.3 ping statistics —
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.033/0.036/0.040/0.007 ms
Linux中需要手动终止命令ctrl+c
也可以使用-c 来指定次数
[root@localhost ~]# ping -c 3 172.17.0.3
PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.031 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.037 ms
64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.033 ms
— 172.17.0.3 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.031/0.033/0.037/0.007 ms
bytes值: 数据包大小,也就是字节。
time值: 响应时间,这个时间越小,说明你连接这个地址速度越快。
TTL值: Time To Live,表示DNS记录在DNS服务器上存在的时间,它是IP协议包的一个值,告诉路由器该数据包何时需要被丢弃。
可以通过Ping返回的TTL值大小,粗略地判断目标系统类型是Windows系列还是UNIX/Linux系列。
默认情况下,Linux系统的TTL值为64或255,WindowsNT/2000/XP系统的TTL值为128,Windows98系统的TTL值为32,UNIX主机的TTL值为255。
因此一般TTL值:
100~130ms之间,Windows系统 ;
240~255ms之间,UNIX/Linux系统。
在最后一行我们看到一个陌生的指标** mdev (Mean Deviation) **
表示 ICMP 包的RTT偏离平均值的程度,主要用来衡量网速的稳定性,mdev值越大说明网络越不稳定。
PING www.a.shifen.com (180.101.49.12) 56(84) bytes of data.
64 bytes from 180.101.49.12 (180.101.49.12): icmp_seq=1 ttl=53 time=15.0 ms
64 bytes from 180.101.49.12 (180.101.49.12): icmp_seq=2 ttl=53 time=15.1 ms
64 bytes from 180.101.49.12 (180.101.49.12): icmp_seq=3 ttl=53 time=14.7 ms
— www.a.shifen.com ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 14.777/14.966/15.122/0.200 ms
TTL是 Time To Live的缩写,该字段指定IP包被路由器丢弃之前允许通过的最大网段数量。
[root@localhost ~]# ping -t 255 baidu.com
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=52 time=26.5 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=52 time=26.5 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=52 time=26.4 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=4 ttl=52 time=26.7 ms
^C
— baidu.com ping statistics —
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 26.488/26.566/26.740/0.153 ms
每2秒ping下百度
[root@localhost ~]# ping -i 2 baidu.com
PING baidu.com (39.156.69.79) 56(84) bytes of data.
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=50 time=38.8 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=50 time=38.8 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=50 time=65.8 ms
^C
— baidu.com ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 4003ms
rtt min/avg/max/mdev = 38.832/47.842/65.833/12.721 ms
一下子ping三次
[root@localhost ~]# ping -f -c 3 220.181.38.148
PING 220.181.38.148 (220.181.38.148) 56(84) bytes of data.
— 220.181.38.148 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 38ms
rtt min/avg/max/mdev = 28.496/28.519/28.538/0.195 ms, pipe 2, ipg/ewma 19.064/28.531 ms
默认情况下,ping命令是以64字节大小的数据包来测试网络连通性的
可以用-s 参数修改字节大小
[root@localhost ~]# ping -s 500 220.181.38.148
PING 220.181.38.148 (220.181.38.148) 500(528) bytes of data.
508 bytes from 220.181.38.148: icmp_seq=1 ttl=52 time=26.9 ms
508 bytes from 220.181.38.148: icmp_seq=2 ttl=52 time=26.9 ms
508 bytes from 220.181.38.148: icmp_seq=3 ttl=52 time=26.8 ms
508 bytes from 220.181.38.148: icmp_seq=4 ttl=52 time=27.0 ms
508 bytes from 220.181.38.148: icmp_seq=5 ttl=52 time=26.8 ms
-i 2 发送周期为 2秒 -s 设置发送包的大小 128 -t 设置TTL值为 255
[root@localhost ~]# ping -i 2 -s 128 -t 255 baidu.com
PING baidu.com (39.156.69.79) 128(156) bytes of data.
136 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=50 time=39.0 ms
136 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=50 time=39.1 ms
136 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=50 time=38.8 ms
^C
— baidu.com ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 4002ms
rtt min/avg/max/mdev = 38.853/39.009/39.109/0.254 ms
判断整个网段所有ip存活情况
利用脚本,获取整个网段ip存活情况
[root@localhost ~]# cat ping.sh
for i in {1…254};
do
ip=192.168.54.$i
ping -c 1 -w 1 -W 1 $ip >/dev/null 2>&1
if [ ? = 0 ] ; t h e n e c h o " ? = 0 ];then echo " ?=0];then echo"ip [yes]"
else
echo “$ip [no]”
fi
done
最小情况是1s,那么整个网段大概就要四分钟
这只是最简单的脚本,可以自己去修改
可以利用ping去大致判断下网络情况