#!/bin/bash




function macv_file_check(){
	#检测网卡文件是否有缺失，缺失的macv文件删除，
	for i in $(ls /etc/sysconfig/network-scripts/ifcfg-macv*| sort -uV)
		do 
			if [[ $(cat $i | wc -l) == 10 ]];then
				echo $i 文件正常
			else 
				echo $i 文件异常
				chattr -i ${i}
				rm -rf $i
			fi
		done

}




function defroute_check()
{
	def_route=`/usr/sbin/ip route | grep default|wc -l`
	if  [[ $def_route -lt 1 ]];then
		if [[ -s "/opt/pppoe/route_mgt.txt" ]];then
			route_mgt_1=`cat /opt/pppoe/route_mgt.txt`
			/usr/sbin/ip route add $route_mgt_1
		fi
	fi
	
	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}'`
		tables_num=`cat /etc/iproute2/rt_tables |grep "250[ ]*mgt"|wc -l`
		if [[ $tables_num -lt 1 ]];then
			echo "250   mgt" >> /etc/iproute2/rt_tables
		fi
		route_num_1=`/usr/sbin/ip route show table mgt 2>>/dev/null|grep "default via $mgt_gateway dev $mgt_dev" |wc -l`
		route_num_2=`/usr/sbin/ip route show table mgt 2>>/dev/null|grep "$mgt_net dev $mgt_dev" |wc -l`
		if [[ $route_num_1 -lt 1 ]];then
			/usr/sbin/ip route add default via $mgt_gateway dev $mgt_dev table mgt
		fi
		if [[ $route_num_2 -lt 1 ]];then
			/usr/sbin/ip route add $mgt_net dev $mgt_dev table mgt
		fi
		rule_num=`/usr/sbin/ip rule | grep "from $mgt_ip lookup mgt"|wc -l`
		if [[ $rule_num -lt 1 ]];then
			/usr/sbin/ip rule add from $mgt_ip lookup mgt
		fi
	fi
}

function checkvlanEth()
{
	num=$1
	colsN=`cat /opt/pppoe/pppoe_user.txt | awk '{print NF}'|sed -n $(($num+1))p`
	#colsN5
	if [[ $colsN -lt 5 ]];then
		ttime=`date +"%Y-%m-%d %H:%M:%S"`
		echo "${ttime}, $num line information error" >> /opt/pppoe/pppoe.log
		exit
	fi
	if [ ! -s /usr/sbin/ifdown-macvlan ];then 
		curl -o /usr/sbin/ifdown-macvlan http://down.9494.org:9999/ppp/ifdown-macvlan && chmod +x /usr/sbin/ifdown-macvlan
	fi
	if [ ! -s /usr/sbin/ifup-macvlan ];then
		curl -o /usr/sbin/ifup-macvlan http://down.9494.org:9999/ppp/ifup-macvlan && chmod +x /usr/sbin/ifup-macvlan
	fi
	vlanid=`cat /opt/pppoe/pppoe_user.txt|sed -n $(($num+1))p|awk '{print $1}'`
	p_eth=`cat /opt/pppoe/pppoe_user.txt|sed -n $(($num+1))p|awk '{print $4}'`
	if [[ $vlanid == "novlan" ]] ;then
		eth_name=`printf "macv%.2d" $num`
		if  [[ ! -f  "/etc/sysconfig/network-scripts/ifcfg-${eth_name}" ]];then
			mac=`dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/' -e 's/^\(.\)[13579bdf]/\10/'`
			echo "DEVICE=${eth_name}" > /etc/sysconfig/network-scripts/ifcfg-${eth_name}
			echo "DEVICETYPE=macvlan" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
			echo "TYPE=macvlan" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
			echo "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
			echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
			echo "NM_CONTROLLED=no" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
			echo "MACVLAN_PARENT=${p_eth}" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
			echo "MACVLAN_MODE=bridge" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
			echo "MACADDR=$mac" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
			echo "DEFROUTE=no" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
			/usr/sbin/ifup-macvlan ${eth_name}
			sleep 1
		else
			macv_num=`/usr/sbin/ip addr|grep ${eth_name}@|wc -l`
			if [[ $macv_num -le 0 ]];then
				/usr/sbin/ifup-macvlan ${eth_name}
				sleep 1
			fi
		fi
	else
		if  [[ ! -f  "/etc/sysconfig/network-scripts/ifcfg-${p_eth}.${vlanid}" ]];then
			mac=`dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/' -e 's/^\(.\)[13579bdf]/\10/'`
			echo "DEVICE=${p_eth}.${vlanid}" > /etc/sysconfig/network-scripts/ifcfg-${p_eth}.${vlanid}
			echo "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-${p_eth}.${vlanid}
			echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-${p_eth}.${vlanid}
			echo "VLAN=yes" >> /etc/sysconfig/network-scripts/ifcfg-${p_eth}.${vlanid}
			echo "MACADDR=$mac" >> /etc/sysconfig/network-scripts/ifcfg-${p_eth}.${vlanid}
			/usr/sbin/ifup ${p_eth}.${vlanid}
			sleep 1
			eth_name=`printf "macv%.2d" $num`
			if  [[ ! -f  "/etc/sysconfig/network-scripts/ifcfg-${eth_name}" ]];then
				mac1=`dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/' -e 's/^\(.\)[13579bdf]/\10/'`
				echo "DEVICE=${eth_name}" > /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "DEVICETYPE=macvlan" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "TYPE=macvlan" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "NM_CONTROLLED=no" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "MACVLAN_PARENT=${p_eth}.${vlanid}" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "MACVLAN_MODE=bridge" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "MACADDR=$mac1" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "DEFROUTE=no" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				/usr/sbin/ifup-macvlan ${eth_name}
				sleep 1
			else
				macv_num=`/usr/sbin/ip addr|grep ${eth_name}@|wc -l`
				if [[ $macv_num -le 0 ]];then
					/usr/sbin/ifup-macvlan ${eth_name}
					sleep 1
				fi
			fi
		else
			vlan_num=`/usr/sbin/ip addr|grep ${p_eth}.${vlanid}@|wc -l`
			if [[ $vlan_num -le 0 ]];then
				/usr/sbin/ifup ${p_eth}.${vlanid}
				sleep 1
			fi
			eth_name=`printf "macv%.2d" $num`
			if  [[ ! -f  "/etc/sysconfig/network-scripts/ifcfg-${eth_name}" ]];then
				mac1=`dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/' -e 's/^\(.\)[13579bdf]/\10/'`
				echo "DEVICE=${eth_name}" > /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "DEVICETYPE=macvlan" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "TYPE=macvlan" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "NM_CONTROLLED=no" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "MACVLAN_PARENT=${p_eth}.${vlanid}" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "MACVLAN_MODE=bridge" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "MACADDR=$mac1" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				echo "DEFROUTE=no" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				/usr/sbin/ifup-macvlan ${eth_name}
				sleep 1
			else
				macv_num=`/usr/sbin/ip addr|grep ${eth_name}@|wc -l`
				if [[ $macv_num -le 0 ]];then
					/usr/sbin/ifup-macvlan ${eth_name}
					sleep 1
				fi
			fi
		fi
	fi	
}

function rule_check()
{
	config_list_r=`ls /etc/sysconfig/network-scripts/ifcfg-ppp*|grep "/etc/sysconfig/network-scripts/ifcfg-ppp[1-9]*[0-9]*$"|sort -Vk 1`
	config_num_r=`ls /etc/sysconfig/network-scripts/ifcfg-ppp*|grep "/etc/sysconfig/network-scripts/ifcfg-ppp[1-9]*[0-9]*$"|sort -Vk 1|wc -l`
	for config_r in $config_list_r
	do
		clo_r=`echo $config_r | sed "s/\/etc\/sysconfig\/network-scripts\/ifcfg-ppp//g"`
		checkvlanEth $clo_r
		pppoe_status=`/usr/sbin/pppoe-status $config_r|grep "Link is up"|wc -l`
		echo $pppoe_status
		if [[ $pppoe_status -gt 0 ]];then
			routenum=`/usr/sbin/ip route show table ppp${clo_r} 2> /dev/null|wc -l`
			if [[ $routenum -lt 1 ]]; then
				ttime=`date +"%Y-%m-%d %H:%M:%S"`
				echo "${ttime} $config routenum 0" >> /opt/pppoe/pppoe.log
				checkroute $clo_r $config_num_r
			else
				checkrule $clo_r $config_num_r
			fi
		fi
	done
}

function checkroute()
{
	num=$1
	dev_name=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-ppp$num|awk '$1~"^[0-9]" && $2~"ppp"{print $2}'|sed 's/://g'`
	rulenum=`/usr/sbin/ip rule | awk '$NF~"^ppp'$num'$"'|wc -l`
	ipaddr=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-ppp$num|awk '$1~"inet$" && $2~"^[0-9]"{print $2 }'`
	ipaddr6=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-ppp$num|awk '$1~"inet6$" && $2~"^[0-9]"{print $2 }'`
	#ipaddr6=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-ppp$num | awk '$1 ~ "inet6$" && $2 ~ "^[0-9]" { sub(/\/.*$/, "", $2); print $2 }'`
	check_tables_num=`cat /etc/iproute2/rt_tables |grep "ppp$num$"|wc -l`
	if [[ $check_tables_num -lt 1 ]];then
		echo "$(($num+10))  ppp${num}" >> /etc/iproute2/rt_tables
	fi
	ip route flush table ppp${num}
	while [ $rulenum -gt 0 ] ;do 
		/usr/sbin/ip rule del lookup ppp${num}
		/usr/sbin/ip -6 rule del lookup ppp${num}
		let rulenum--
	done
	/usr/sbin/ip route add 0/0 dev $dev_name table ppp$num
	/usr/sbin/ip -6 route add 0/0 dev $dev_name table ppp$num  
	/usr/sbin/ip rule add from $ipaddr table ppp$num pref 200
	/usr/sbin/ip -6 rule add from $ipaddr6 table ppp$num pref 200
}

function checkrule()
{
	num=$1
	dev_name_1=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-ppp$num|awk '$1~"^[0-9]" && $2~"ppp"{print $2}'|sed 's/://g'`
	ipaddr=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-ppp$num|awk '$1~"inet$" && $2~"^[0-9]"{print $2 }'`
	ipaddr6=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-ppp$num|awk '$1~"inet6$" && $2~"^[0-9]"{print $2 }'`
	#ipaddr6=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-ppp$num | awk '$1 ~ "inet6$" && $2 ~ "^[0-9]" { sub(/\/.*$/, "", $2); print $2 }'`
	rulenum=`/usr/sbin/ip rule | awk '$NF~"^ppp'$num'$"'|wc -l`   
	#rulenum1=`/usr/sbin/ip rule | grep "from all fwmark $mark_hex lookup ppp$dev_num_1"|wc -l`
	rulenum2=`/usr/sbin/ip rule | grep "from $ipaddr lookup ppp$num"|wc -l`
	if [[ rulenum2 -eq 0 ]];then
		while [ $rulenum -gt 0 ]
		do 
			/usr/sbin/ip rule del lookup ppp$num
			/usr/sbin/ip -6 rule del lookup ppp$num
			let rulenum--
		done
		/usr/sbin/ip rule add from $ipaddr table ppp$num pref 200
		/usr/sbin/ip -6 rule add from $ipaddr6 table ppp$num pref 200
	fi
}



#############################################################################
#检测网卡文件是否有缺失
macv_file_check




defroute_check
rule_check



