如何批量ping地址檢視網路是否暢通
測試環境搬遷後,需要批量去ping所有機器的IP,看網路是否暢通
測試思路:
[[email protected]_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
[[email protected]_BankVerify luyantest]$ ping -c 1 172.29.111.118 | grep loss
1 packets transmitted, 0 received, 100% packet loss, time 10000ms
[[email protected]_BankVerify luyantest]$ ping -c 1 172.29.111.118 | grep loss | awk '{print $6}'
100%
[[email protected]_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