测试环境搬迁后,需要批量去ping所有机器的IP,看网络是否畅通

测试思路:

[weblogic@pays03pre_BankVerify luyantest]$ ping -c 1 172.29.111.118
PING 172.29.111.118 (172.29.111.118) 56(84) bytes of data.

--- 172.29.111.118 ping statistics ---

1 packets transmitted, 0 received, 100% packet loss, time 10000ms

[weblogic@pays03pre_BankVerify luyantest]$ ping -c 1 172.29.111.118 | grep loss
1 packets transmitted, 0 received, 100% packet loss, time 10000ms

[weblogic@pays03pre_BankVerify luyantest]$ ping -c 1 172.29.111.118 | grep loss | awk '{print $6}'
100%
[weblogic@pays03pre_BankVerify luyantest]$ ping -c 1 172.29.111.118 | grep loss | awk '{print $6}' | awk -F "%" '{print $1}'
100

如上发现,如果ping一个ip地址没有通过,那么丢包率就是100%。这样我们可以循环去ping所有的地址,然后截取丢包率,去对比

脚本如下:

#! /bin/bash
#set -x
for i in `cat iplist`
do
ping=`ping -c 1 $i|grep loss|awk '{print $6}'|awk -F "%" '{print $1}'`
if [ $ping -eq 100 ]
then
echo ping $i fail
else
echo ping $i ok
fi
done
#set +x

05-14 06:10