#!/bin/bash

DNS1="223.6.6.6"
DNS2="223.5.5.5"

function default_route()
{
	def_route=`/usr/sbin/ip route | grep default | head -1|grep "default dev ppp[0-9]*[0-9]* scope link"|wc -l`
	if [[ $def_route -lt 1 ]];then
		set_def_route
	else
		ppp_def_name=`/usr/sbin/ip route | grep default | head -1|grep "default dev ppp[0-9]*[0-9]* scope link"|awk '{print $3}'`
		eth_ip=$(ip route  | grep "$ppp_def_name\b" | grep src | awk '{print $NF}')
		#ppp_def_name是网卡名，不是ifcfg-ppp的名字
		#eth_ip=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-${ppp_def_name}  2> /dev/null|awk '$1~"inet$" && $2~"^[0-9]"{print $2 }'`
		if [[ $eth_ip != "" ]];then
			ppp_def_net=`ping -c 5 -w 5 -A -I $eth_ip ${DNS2}   2> /dev/null | grep  'trans' | grep -Eo '[0-9]+%'  | tr -cd '[0-9]'`
			if [[ $ppp_def_net == 100 || -z $ppp_def_net ]];then
				echo "ifcfg-${ppp_def_name} ${eth_ip} no network" >> /opt/pppoe/route.log
				# 删除IPv4默认路由
				/usr/sbin/ip route del default dev ${ppp_def_name} scope link
				# 删除IPv6默认路由
				/usr/sbin/ip -6 route del default dev ${ppp_def_name} scope link
				set_def_route
				#echo "no network"
			fi
		fi
		
	fi
	
}

function set_def_route()
{
	check_network
	#tables_ppp=`cat /etc/iproute2/rt_tables|grep "ppp[0-9]*[0-9]*"|awk '{print $2}'`
	ppps_config=`ls /etc/sysconfig/network-scripts/ifcfg-ppp*|grep "/etc/sysconfig/network-scripts/ifcfg-ppp[1-9]*[0-9]*$"|sort -Vk 1`
	ppps=`echo $ppps_config | sed "s/\/etc\/sysconfig\/network-scripts\/ifcfg-//g"`
	for ppp_config_name in $ppps
	do
		pppoe_status_1=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-${ppp_config_name}|grep "Link is up"|wc -l`
		echo $pppoe_status_1
		if [[ $pppoe_status_1 -gt 0 ]];then
			# 获取PPP设备名称
			dev_name_1=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-${ppp_config_name}|awk '$1~"^[0-9]" && $2~"ppp"{print $2}'|sed 's/://g'`
			# 获取PPP接口IPv6地址
			ipaddr6=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-${ppp_config_name}|awk '$1~"inet6$" && $2~"^[0-9]"{print $2 }'`
			table_route=`/usr/sbin/ip route show table $ppp_config_name|wc -l`
			if [[ $table_route -gt 0 ]]; then
				ppp_net=`cat /opt/pppoe/pppnet/${ppp_config_name} 2> /dev/null`
				if [[ $ppp_net == 100 || -z $ppp_net ]];then
					continue
				else
					echo "ifcfg-${ppp_config_name} ${dev_name_1} loss:${ppp_net} set_def_route" >> /opt/pppoe/route.log
					# 添加IPv4默认路由
					/usr/sbin/ip route add default dev ${dev_name_1} scope link
					# 如果获取到了IPv6地址，添加IPv6默认路由
					if [[ -n "$ipaddr6" ]]; then
						# 添加IPv6默认路由到路由表
						/usr/sbin/ip -6 route add default dev ${dev_name_1} table ${ppp_config_name}
					fi
					/usr/bin/systemctl restart cgroupv2
					break
				fi
			fi
		fi
	done
	def_route_1=`/usr/sbin/ip route | grep default | head -1|grep "default dev ppp[0-9]*[0-9]* scope link"|wc -l`
	if [[ $def_route_1 -lt 1 ]];then
		if [[ -s "/opt/pppoe/route_mgt.txt" ]];then
			route_mgt=`cat /opt/pppoe/route_mgt.txt`
			def_route_mgt=`/usr/sbin/ip route | grep default | head -1|grep "$route_mgt"|wc -l`
			if  [[ $def_route_mgt -lt 1 ]];then
				/usr/sbin/ip route add $route_mgt
			fi
		else
			if [[ -s "/opt/pppoe/route.txt" ]];then
				mgt_gateway=`cat /opt/pppoe/route.txt|sed -n 1p|awk '{print $1}'`
				mgt_dev=`cat /opt/pppoe/route.txt|sed -n 2p|awk '{print $1}'`
				mgt_ip=`cat /opt/pppoe/route.txt|sed -n 3p|awk '{print $1}'`
				mgt_net=`cat /opt/pppoe/route.txt|sed -n 4p|awk '{print $1}'`
				metr=`/usr/sbin/ip route | grep "$mgt_net dev $mgt_dev"|awk -F ' ' '{print $NF}'`
				echo "default via $mgt_gateway dev $mgt_dev proto dhcp metric $metr" > /opt/pppoe/route_mgt.txt
				route_mgt=`cat /opt/pppoe/route_mgt.txt`
				def_route_mgt=`/usr/sbin/ip route | grep default | head -1|grep "$route_mgt"|wc -l`
				if  [[ $def_route_mgt -lt 1 ]];then
					/usr/sbin/ip route add $route_mgt
				fi
			fi
		fi
			
	fi
}



function check_network()
{
	if [[ ! -d "/opt/pppoe/pppnet" ]];then
		mkdir -p /opt/pppoe/pppnet
	fi
	#ppps=`cat /etc/iproute2/rt_tables|grep "ppp[0-9]*[0-9]*"|awk '{print $2}'`
	ppps_config=`ls /etc/sysconfig/network-scripts/ifcfg-ppp*|grep "/etc/sysconfig/network-scripts/ifcfg-ppp[1-9]*[0-9]*$"|sort -Vk 1`
	ppps=`echo $ppps_config | sed "s/\/etc\/sysconfig\/network-scripts\/ifcfg-//g"`
	for p in $ppps
	do
		ppp_status=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-${p}|grep "Link is up"|wc -l`
		if [[ $ppp_status -gt 0 ]];then
			eth_ip=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-${p}|awk '$1~"inet$" && $2~"^[0-9]"{print $2 }'`
			ping -c 5 -w 5 -A -I $eth_ip ${DNS2}   2> /dev/null | grep  'trans' | grep -Eo '[0-9]+%'  | tr -cd '[0-9]' > /opt/pppoe/pppnet/${p} &
		fi
	done
	wait
}

default_route
