多wan口鏈路備份腳本實現

#!/bin/sh /etc/rc.common
START=101

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

start()
{
	/usr/bin/wan_module_monitor &
}

stop()
{
        ps |grep -v grep|grep wan_module_monitor | while read p u o
	do
	kill -9 $p
	done
}

default_route_set()
{

	. /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}')
	metric=$(ubus call network.interface.$name status | grep -w "metric" | grep -oE '([0-9])')
	
	if [ $gw ]; then 
	option_cb(){ 	
	
		if [ "$1" = "ifname" ] && [ $gw ] && [ "$2" != "lo" ] && [ "$2" != "br-lan" ]; then 

			route add default gw $gw dev $2 

			if ping -c2 -I $2 ${ipaddr1} | grep -q 'ttl='; then
				while route | grep "default" | grep "$2" > /dev/null
				do
					route del default dev $2
				done
				route add default gw $gw dev $2 metric $metric	
			elif ping -c2 -I $2 ${ipaddr2} | grep -q 'ttl='; then
				while route | grep "default" | grep "$2" > /dev/null
				do
					route del default dev $2
				done
				route add default gw $gw dev $2 metric $metric
			else
				while route | grep "default" | grep "$2" > /dev/null
				do
					route del default dev $2
				done
			fi
		fi

	} 
	
	fi
		
fi 

} 

        config_load 'network'
}


restart()
{
	default_route_set
}

由於協議棧獲取不到接口up/down狀態,下一篇會給出監控程序

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