View Code BASH
#!/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 |
其它的也有这样的,如下,都是用一个循环去判断而已:
View Code BASH
#!/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 |
相关博文
使用ping命令进行连通性校验的shell脚本