多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状态,下一篇会给出监控程序

你可能感兴趣的:(多wan口链路备份脚本实现)