×

使用ping命令进行连通性校验的shell脚本

hqy hqy 发表于2019-03-19 23:22:00 浏览2225 评论0

抢沙发发表评论

#!/bin/sh


IP="10.10.100.1"


kill_deamon() {


/etc/init.d/deamon stop

}


start_deamon(){

/etc/init.d/deamon start

}



check_ping() {


local total="0"

local i="0"


while [ $i -lt 10 ]; do


local line=""

#echo "ping $IP"

line=`ping $IP -c 1 -s 1 -W 1 | grep "100% packet loss" | wc -l`

if [ "${line}" != "0" ]; then

echo "ping failed!"

total=$((total+1))

else

echo "ping ok!"

total="0"

fi

i=$((i+1))

sleep 1

done


if [ $total -gt 5 ]; then

echo "check failed!"

return 1

else 

echo "check ok!"

return 0

fi

}



start(){


local rtl=""

while [ 1 ]; do

check_ping

rtl=$?

if [ "$rtl" != "0" ]; then

echo " restart deamon start "

kill_deamon

start_deamon

else

sleep 600;

fi

done

}


start

其它的也有这样的,如下,都是用一个循环去判断而已:


#!/bin/sh

 

line="0"

total="0"

dst=`uci get network.mesh.gateway`

 

while [ 1 ]; do

line=`ping $dst -c 1 -s 1 -W 1 | grep "100% packet loss" | wc -l`

if [ "${line}" != "0" ]; then

total=$((total+1))

else

total="0"

fi

 

if [ "${total}" == "5" ]; then

reboot -f

fi

 

sleep 12

done


另外一个:


#!/bin/bash


for siteip in $(seq 1 254)do

    site="192.168.1.${siteip}"

    ping -c1 -W1 ${site} &> /dev/null

    if [ "$?" == "0" ]; then

        echo "$site is UP"

    else

        echo "$site is DOWN"

    fi

done


打赏

本文链接:https://www.kinber.cn/post/390.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

 您阅读本篇文章共花了: 

群贤毕至

访客