多wan口鏈路連接監控程序

#!/bin/sh /etc/rc.common

ipaddr1="www.baidu.com"
ipaddr2="www.163.com"
netstat=0

start()
{
	. /lib/functions/network.sh

config_cb(){ 

local type=$1 
local name=$2 


if [ "$1" = "interface" ] && [ "$2" != "loopback" ]&& [ "$2" != "lan" ]; then 
	 
	gw=$(ubus call network.interface.$name  status | grep nexthop | grep -oE '([0-9]{1,3}.){3}.[0-9]{1,3}')
	if [ $gw ]; then	
	option_cb(){ 	
	
		if [ "$1" = "ifname" ] && [ $gw ] && [ "$2" != "lo" ] && [ "$2" != "br-lan" ]; then 
			time=`date`

			if ping -c2 -I $2 ${ipaddr1} | grep -q 'ttl='; then
				netstat=1
				echo "$time $2 ping ${ipaddr1} $name is alive netstat=${netstat}" >> /pinglog				
			elif ping -c2 -I $2 ${ipaddr2} | grep -q 'ttl='; then
				netstat=1
				echo "$time $2 ${ipaddr2} $name is alive netstat=${netstat}" >> /pinglog	
			else
				#route add default gw $gw dev $2
				
				if ping -c2 -I $2 $gw | grep -q 'ttl='; then
					netstat=0
					echo "$time $2 ping $gw $name is alive netstat=${netstat}" >> /pinglog	
				elif ping -c2 -I $2 $gw | grep -q 'ttl='; then
					netstat=0
					echo "$time $2 ping $gw $name is alive netstat=${netstat}" >> /pinglog
				else
					echo "$time $2 ping $gw $name not alive netstat=${netstat}" >> /pinglog
					while route | grep "default" | grep "$2" > /dev/null
					do
						route del default dev $2
					done
				fi
				
				if  [ "$netstat" = "0" ]; then
					time=`date`
					echo "$time wan_backuplink init restart !" >> /pinglog	
					/etc/init.d/wan_backuplink restart
				fi
				
			fi
		fi

	} 
	fi
			
fi 

} 
	config_load 'network'
        
}

while true
do
	echo $netstat >>/pinglog
	if  [ "$netstat" = "0" ]; then
		time=`date`
		echo "$time wan_backuplink init restart !" >> /pinglog	
		/etc/init.d/wan_backuplink restart
	fi
	sleep 5
	start
	
done

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章