耗时分析:
无法ping通的IP,耗时20sec.
可以ping通的IP,耗时1sec.
https://blog.csdn.net/weixin_42126942/article/details/123518459?spm=1001.2014.3001.5501
代码:
#!/bin/bash \n
# =========================================================`
#!/bin/bash
# =========================================================
# pingIP_proc.sh
# test the ping in autoMode
# variable in {3}:
# 0 - The 1st - 3st field of ip_addr: such as 172.16.16
# 1 - The last field of Start_IP
# 2 - The last field of End_IP
# =========================================================
`RED_COLOR='\E[1;31m' #红
GREEN_COLOR='\E[1;32m' #绿
YELOW_COLOR='\E[1;33m' #黄
BLUE_COLOR='\E[1;34m' #蓝
PINK='\E[1;35m' #粉红
RES='\E[0m'
function echo_color(){
colorType=$1
echoContext=$2
if [ $colorType -eq 0 ];then
echo -e "${GREEN_COLOR}$echoContext ${RES}"
elif [ $colorType -eq 1 ];then
echo -e "${RED_COLOR}$echoContext ${RES}"
elif [ $colorType -eq 2 ];then
echo $echoContext
elif [ $colorType -eq 3 ];then
echo -e "${BLUE_COLOR}$echoContext ${RES}"
else
echo -e "${YELOW_COLOR}$echoContext ${RES}"
fi
}
ping_Ok_cnt=0
pingOKIP[0]='172.16.16.0'
function pingProc(){
for ((i=${1};i<=${2};i++))
do
timeStart=$(date "+ %H:%M:%S")
ip_dst=${ip_first_three_field}.${i}
echo_color 3 "ping ${ip_dst} start on ${timeStart}"
ping=`ping -c 1 $ip_dst|grep loss`
pingRslt=`ping -c 1 $ip_dst|grep loss|awk '{print $6}'|awk -F "%" '{print $1}'`
if [ $pingRslt -eq 100 ];then
echo_color 1 "ping ${ip_dst} fail"
else
echo_color 0 "ping ${ip_dst} OK"
pingOKIP[ping_Ok_cnt]=${ip_dst}
ping_Ok_cnt=`expr $ping_Ok_cnt + 1`
fi
#echo ping $ip_dst end
done
}
# 1. check the input variable
if [ $# -eq 3 ];then
ip_first_three_field=$1;
ip_start=$2;
ip_end=$3;
else
echo "==========================================="
echo_color 4 "There is not enough input var ...... $#"
echo_color 4 "There need 3 input var ....."
echo " 0 - IP field of 1 -3: such as: 172.16.16"
echo " 1 - The last field of Start_IP"
echo " 2 - The last field of End_IP "
echo "==========================================="
exit
fi
# 2. Call the ping function
timeStart_init=$(date "+%Y-%m-%d %H:%M:%S")
echo $timeStart_init
pingProc $ip_start $ip_end
timeEnd=$(date "+%Y-%m-%d %H:%M:%S")
echo Start: $timeStart_init End: $timeEnd
# 3. output the ping result
echo_color 4 "ping OK Cnt: [${ping_Ok_cnt}], the IP list:"
ping_OK_num=`expr $ping_Ok_cnt - 1`
for i in $(seq 0 $ping_OK_num)
do
echo " $i: [ ${pingOKIP[$i] }]"
done