Linux下用shell脚本ping多个ip

在用谷歌的一些应用的时候需要用到其dns轮询后的多个ip,但是有些被墙,如何快速确定哪些没有被墙。

第一个想到就是用linux下shell脚本,该脚本可以实现多次运行,ip可以作为参数跟在后面,方便多处运行,下面给出代码:

#!/bin/bash
checkalive (){
NODE=$1
ping -c 3 $NODE>/dev/null 2>&1
if [ $? -eq 0 ]
then
echo "$NODE is alive"
else
echo "$NODE is not alive"
fi
}
input="$1"
ips=$(echo ${input%.*}.)
start=$(echo ${input##*.} | awk -F- '{print $1}')
end=$(echo ${input##*.} | awk -F- '{print $2}')
echo $start
echo $end
for(( i=$start;i<=$end;i++ ))
do
ip="$ips$i"
checkalive  $ip
done

用ping命令加-c参数进行设置测试次数,可以修改该值加快ping检测速度,当然牺牲准确率(不排除丢包可能)。把显示重定向到null里去,并读取脚本后面参数。

用法:保存至脚本文件pip.sh,赋予执行权限运行

./pip.sh 72.14.203.1-19

返回结果:

1
19
72.14.203.1 is not alive
72.14.203.2 is not alive
72.14.203.3 is not alive
72.14.203.4 is alive
72.14.203.5 is alive
72.14.203.6 is alive
72.14.203.7 is not alive
72.14.203.8 is not alive
72.14.203.9 is not alive
72.14.203.10 is not alive
72.14.203.11 is not alive
72.14.203.12 is not alive
72.14.203.13 is not alive
72.14.203.14 is not alive
72.14.203.15 is not alive
72.14.203.16 is not alive
72.14.203.17 is alive
72.14.203.18 is alive
72.14.203.19 is alive

此条目是由 malu8 发表在 未分类 分类目录的。将固定链接加入收藏夹。

评论已关闭。