#!/bin/bash
IFS=$'\n'
file=/var/log/import.log
hour=$(date +"%Y-%m-%d %H:%M")
uuid=$(cat /etc/salt/minion_id)
echo "${hour}   UUID:${uuid}	执行ppp做默认路由的主机拨号" | tee -a $file





function check_ppp()
{
	pppd_num=`/usr/bin/ps aux|grep "/usr/sbin/pppd "|grep -v "grep"|wc -l`
	if [ $pppd_num -gt 0 ];then
		echo "pppd already in use"
		exit
	fi
}

function check_pppoe_config()
{
	if [[ ! -s "/etc/rsyslog.d/pppd.conf" ]];then
		curl -sSL https://automateplus.oss-cn-hangzhou.aliyuncs.com/rsyslog.sh|sh
	fi
	if [[ ! -d "/opt/pppoe" ]];then
		mkdir -p /opt/pppoe
		echo "/opt/pppoe is created,have no file /opt/pppoe/pppoe_user.txt"
		exit
	else
		if [[ ! -s "/opt/pppoe/pppoe_user.txt" ]];then
			echo "/opt/pppoe/pppoe_user.txt information error"
			exit
		fi
	fi
	if [[ ! -s "/usr/sbin/pppoe" ]];then
		yum install -y rp-pppoe
	fi
	if [[ ! -s "/opt/pppoe/route.txt" ]];then
	    mgt_gateway=`/usr/sbin/ip route | grep default | awk '{print $3}' | head -1`
		mgt_dev=`/usr/sbin/ip route | grep default | awk '{print $5}' | head -1`
		mgt_ip=`/usr/sbin/ip addr show dev $mgt_dev | grep -w "inet" | awk '{print $2}' | awk -F/ '{print $1}'`
		mgt_net=`/usr/sbin/ip route | grep -w $mgt_dev | grep -w $mgt_ip | awk '{print $1}'`
		echo $mgt_gateway > /opt/pppoe/route.txt
		echo $mgt_dev >> /opt/pppoe/route.txt
		echo $mgt_ip >> /opt/pppoe/route.txt
		echo $mgt_net >> /opt/pppoe/route.txt
	fi
	if [[ ! -s "/opt/pppoe/route_mgt.txt" ]];then
		mgt_route=`/usr/sbin/ip route | grep default|head -1`
		echo $mgt_route > /opt/pppoe/route_mgt.txt
	fi
	if [[ ! -s "/opt/pppoe/route6.txt" ]];then
	    mgt_gateway6=`/usr/sbin/ip -6 route | grep default | awk '{print $3}' | head -1`
		mgt_dev6=`/usr/sbin/ip -6 route | grep default | awk '{print $5}' | head -1`
		mgt_ipv6=`/usr/sbin/ip -6 addr show dev $mgt_dev | grep -w "inet" | awk '{print $2}' | awk -F/ '{print $1}'`
		mgt_net6=`/usr/sbin/ip -6 route | grep -w $mgt_dev | grep -w $mgt_ip | awk '{print $1}'`
		echo $mgt_gateway5 > /opt/pppoe/route6.txt
		echo $mgt_dev5 >> /opt/pppoe/route6.txt
		echo $mgt_ipv6 >> /opt/pppoe/route6.txt
		echo $mgt_net5 >> /opt/pppoe/route6.txt
	fi
	if [[ ! -s "/opt/pppoe/route_mgt5.txt" ]];then
		mgt_route6=`/usr/sbin/ip -6 route | grep default|head -1`
		echo $mgt_route6 > /opt/pppoe/route_mgt6.txt
	fi
	cat  > /root/ifcfg-ppp0 << EOF
USERCTL=yes
BOOTPROTO=dialup
NAME=DSLppp0
DEVICE=ppp0
TYPE=xDSL
ONBOOT=no
PIDFILE=/var/run/pppoe-adsl.pid
FIREWALL=NONE
PING=.
PPPOE_TIMEOUT=80
LCP_FAILURE=6
LCP_INTERVAL=10
CLAMPMSS=1412
CONNECT_POLL=6
CONNECT_TIMEOUT=60
DEFROUTE=no
SYNCHRONOUS=no
ETH=wan0
PROVIDER=DSLppp0
USER=1234567
PEERDNS=no
DEMAND=no
LINUX_PLUGIN=/usr/lib64/pppd/2.4.5/rp-pppoe.so
PPPD_EXTRA="+ipv6 unit 0"
EOF
	systemctl stop firewalld
	systemctl disable firewalld
	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 create_vlan()
{
	curl -sSL -o /opt/pppoe/host_pppoe_vlan.sh https://automateplus.oss-cn-hangzhou.aliyuncs.com/host_pppoe_vlan.sh
	sed -i "/\/opt\/pppoe\/host_pppoe_vlan/d" /etc/rc.d/rc.local
	echo "sh /opt/pppoe/host_pppoe_vlan.sh" >> /etc/rc.d/rc.local
	chmod +x /opt/pppoe/host_pppoe_vlan.sh
	chmod +x /etc/rc.d/rc.local
	sh /opt/pppoe/host_pppoe_vlan.sh
	cols_num=`cat /opt/pppoe/pppoe_user.txt | awk '{print NF}'`
	line_num=1
	for col in $cols_num
	do
		if [[ $col -lt 5 ]];then
			echo "$line_num line information error"
			exit
		fi
		let line_num=line_num+1
	done
	num=`cat /opt/pppoe/pppoe_user.txt |wc -l`
	for i in `seq 0 $(($num-1))`
	do
		if  [[ -f  "/etc/sysconfig/network-scripts/ifcfg-ppp${i}" ]];then
			rm -rf /etc/sysconfig/network-scripts/ifcfg-ppp${i}
		fi
		cp /root/ifcfg-ppp0 /etc/sysconfig/network-scripts/ifcfg-ppp${i}
		sed -i "s/pppoe-adsl.pid/pppoe-adsl${i}.pid/g" /etc/sysconfig/network-scripts/ifcfg-ppp${i}
		sed -i "s/ppp0/ppp${i}/g" /etc/sysconfig/network-scripts/ifcfg-ppp${i}
		pppoe_eth=`printf "macv%.2d" $i`
		sed -i "s/wan0/${pppoe_eth}/g" /etc/sysconfig/network-scripts/ifcfg-ppp${i}
		user=`cat /opt/pppoe/pppoe_user.txt|sed -n $(($i+1))p|awk '{print $2}'`
		passw=`cat /opt/pppoe/pppoe_user.txt|sed -n $(($i+1))p|awk '{print $3}'`
		sed -i "s/1234567/${user}/g" /etc/sysconfig/network-scripts/ifcfg-ppp${i}
		sed -i "/\"${user}\"/d" /etc/ppp/chap-secrets
		sed -i "/\"${user}\"/d" /etc/ppp/pap-secrets
		echo "\"${user}\" *  \"$passw\"" >> /etc/ppp/chap-secrets
		echo "\"${user}\" *  \"$passw\"" >> /etc/ppp/pap-secrets
	done
	for i in `seq 0 $(($num-1))`
	{
		ifup ppp${i} &
	}
	wait
	for i in `seq 0 $(($num-1))`
	{
		#ifup ppp${i}
		pppoe_status=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-ppp${i}|grep "Link is up"|wc -l`
		echo $pppoe_status
		if [[ $pppoe_status -gt 0 ]];then
			dev_name=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-ppp${i}|awk '$1~"^[0-9]" && $2~"ppp"{print $2}'|sed 's/://g'`
			eth_ip=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-ppp${i}|awk '$1~"inet$" && $2~"^[0-9]"{print $2 }'`
			sed -i "/ppp${i}/d" /etc/iproute2/rt_tables
			echo "$(($i+10))   ppp${i}" >> /etc/iproute2/rt_tables
			ip route flush table ppp${i}
			ip route add 0/0 dev ${dev_name} table ppp${i}
			route_num=`ip route show table ppp${i}|wc -l`
			if [[ $route_num==1 ]];then
				echo "${dev_name} add success"
			fi
			rulenum=`/usr/sbin/ip rule | awk '$NF~"^ppp'$i'$"'|wc -l` 
			while [ $rulenum -gt 0 ]
			do 
				ip rule del lookup ppp${i}
				let rulenum--
			done
			ip rule add from $eth_ip table ppp${i} pref 200
		fi
	}
}

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
		tables_ppp=`cat /etc/iproute2/rt_tables|grep "ppp[0-9]*[0-9]*"|awk '{print $2}'`
		#num=`cat /opt/pppoe/pppoe_user.txt |wc -l`
		for x in $tables_ppp
		do
			pppoe_status_1=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-${x}|grep "Link is up"|wc -l`
			echo $pppoe_status_1
			if [[ $pppoe_status_1 -gt 0 ]];then
				dev_name_1=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-${x}|awk '$1~"^[0-9]" && $2~"ppp"{print $2}'|sed 's/://g'`
				table_route=`/usr/sbin/ip route show table $x|wc -l`
				if [[ $table_route -gt 0 ]]; then
					/usr/sbin/ip route add default dev ${dev_name_1} scope link
					break
				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	
	fi
	
}

function pppoe_crontab()
{
	curl -o /opt/pppoe/ppp_route_check.sh https://automateplus.oss-cn-hangzhou.aliyuncs.com/ppp_route_check.sh
	curl -o /opt/pppoe/ppp_check.sh https://automateplus.oss-cn-hangzhou.aliyuncs.com/ppp_check.sh
	curl -o /opt/pppoe/ppp_rule_check.sh https://automateplus.oss-cn-hangzhou.aliyuncs.com/ppp_rule_check.sh
	curl -o /opt/pppoe/check_pppoe_config.sh https://automateplus.oss-cn-hangzhou.aliyuncs.com/check_pppoe_config.sh
	curl -o /opt/pppoe/ppp_nomgt.sh https://automateplus.oss-cn-hangzhou.aliyuncs.com/ppp_nomgt.sh
	chmod +x /opt/pppoe/ppp_check.sh
	chmod +x /opt/pppoe/ppp_rule_check.sh
	chmod +x /opt/pppoe/check_pppoe_config.sh
	chmod +x /opt/pppoe/ppp_nomgt.sh
	sed -i "/vdp_pppoe_check/d" /var/spool/cron/root
	sed -i "/ppp_check/d" /var/spool/cron/root
	echo "2-59/5 * * * * sh /opt/pppoe/ppp_check.sh" >> /var/spool/cron/root
	
	sed -i "/ppp_rule_check/d" /var/spool/cron/root
	echo "*/1 * * * * sh /opt/pppoe/ppp_rule_check.sh" >> /var/spool/cron/root
	
	sed -i "/check_pppoe_config/d" /var/spool/cron/root
	echo "*/1 * * * * sh /opt/pppoe/check_pppoe_config.sh" >> /var/spool/cron/root
	
	sed -i "/ppp_route_check.sh/d" /var/spool/cron/root
	echo "*/1 * * * * sh /opt/pppoe/ppp_route_check.sh" >> /var/spool/cron/root
	
	sed -i "/vdp_pppoe_check/d" /etc/rc.d/rc.local
	sed -i "/ppp_check/d" /etc/rc.d/rc.local
	echo "sed -i '/ppp_check.sh/d' /var/spool/cron/root" >> /etc/rc.d/rc.local
	echo "sed -i '/vdp_pppoe_check.sh/d' /var/spool/cron/root" >> /etc/rc.d/rc.local
	
	sed -i "/ppp_rule_check/d" /etc/rc.d/rc.local
	echo "sed -i '/ppp_rule_check.sh/d' /var/spool/cron/root" >> /etc/rc.d/rc.local
	
	sed -i "/route.txt/d" /etc/rc.d/rc.local
	echo "rm -rf /opt/pppoe/route.txt" >> /etc/rc.d/rc.local
	
	sed -i "/route_mgt.txt/d" /etc/rc.d/rc.local
	echo "rm -rf /opt/pppoe/route_mgt.txt" >> /etc/rc.d/rc.local
	
	sed -i "/vdp_pppoe.sh/d" /etc/rc.d/rc.local
	sed -i "/ppp.sh/d" /etc/rc.d/rc.local
	sed -i "/ppp_nomgt.sh/d" /etc/rc.d/rc.local
	echo "sh /opt/pppoe/ppp_nomgt.sh" >> /etc/rc.d/rc.local
	chmod +x /etc/rc.d/rc.local
}


function pppoe_double_vlan()
{

	#!/bin/bash
	#2023-0615
	#先执行主机拨号脚本，再使用以下脚本
	#name:pppoe_double_vlan.sh

	cat /var/spool/cron/root |grep  -Ec "pppd_check.sh|ppp_check.sh"
	if [ $? != 0 ] ;then
	echo "no  host_ppp init"
	exit;
	fi

	cat /opt/pppoe/pppoe_user.txt |awk '{print $1}' |grep '\.' >/dev/null
	if [ $? == 0 ] ;then 
		echo "double vlan "
		for veth in `cat /opt/pppoe/pppoe_user.txt |awk '{print $1}' |grep '\.'` ;do
			ifcfg_eth=`echo $veth |awk -F '\\.' '{print $1}'`
			py_eth=`cat /opt/pppoe/pppoe_user.txt |grep $veth |awk  '{print $4}'`
			double_vlan_file_num=`ls /etc/sysconfig/network-scripts/ifcfg-${py_eth}.$ifcfg_eth |wc -l` 
			if  [ $double_vlan_file_num -lt 1 ] ;then 
				cp /etc/sysconfig/network-scripts/ifcfg-${py_eth}.$veth  /etc/sysconfig/network-scripts/ifcfg-${py_eth}.$ifcfg_eth
				sed -i '/DEVICE=/d' /etc/sysconfig/network-scripts/ifcfg-${py_eth}.$ifcfg_eth
				sed -i '/MACADDR=/d' /etc/sysconfig/network-scripts/ifcfg-${py_eth}.$ifcfg_eth
				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=${py_eth}.$ifcfg_eth" >> /etc/sysconfig/network-scripts/ifcfg-${py_eth}.$ifcfg_eth
				echo "MACADDR=$mac" >> /etc/sysconfig/network-scripts/ifcfg-${py_eth}.$ifcfg_eth
				
				/usr/sbin/ifup  ifcfg-${py_eth}.$ifcfg_eth
				/usr/sbin/ifup ifcfg-${py_eth}.$veth 
			else 
				echo /etc/sysconfig/network-scripts/ifcfg-${py_eth}.$ifcfg_eth exits;
		
			fi
			
		done
		
	else
		echo "no double vlan;exit"
		exit
	fi


}


check_ppp
check_pppoe_config
create_vlan


default_route
pppoe_crontab
#pppoe_double_vlan