从本地文件中加载ip地址清单,全ip扫描,如果所有ip ping失败多次,执行某个任务:
#! /bin/bash
#ping module:testPing ipAddr
testPing(){
local ipAddr=$1
ping -w 1 -c 2 $ipAddr > /dev/null
local result=$?
if [ $result -eq 0 ]
then
echo "1"
else
echo "0"
fi
}
#ip list from local conf
#testPing localConf
testFromConf(){
local localConf="$1"
for ipAddr in `cat $localConf`
do
local result=`testPing $ipAddr`
if [ $result -eq "1" ]
then
#echo $ipAddr":true"
#ping one addr success
echo "1"
return
#else
#echo $ipAddr":false"
fi
done
#all addrs ping failed
echo "0";
}
#ping loop
#testPingLoop maxCount conf
testPingLoop(){
local maxCount=$1
local conf=$2
for((i=0;i<$maxCount;++i))
do
local result=`testFromConf $conf`
if [ $result -eq "1" ]
then
#some task sucess,exit this ping loop
echo "1"
return
#else
#
fi
done
#has retry maxCount,failed
echo "0"
}
taskIfFailed(){
echo "need reboot?"
}
pingTask(){
local maxCount=$1
local conf=$2
while true
do
local result=`testPingLoop $maxCount $conf`
if [ $result -eq "1" ]
then
#if success, sleep some seconds
echo "success"
sleep 1
else
taskIfFailed
fi
done
}
main(){
#testPing "127.0.0.1"
#testPing "172.1.1.1"
#testFromConf "pingtest.txt"
pingTask 1 "pingtest.txt"
}
main