#!/bin/bash
IFS=$'\n'
file=/var/log/import.log
hour=$(date +"%Y-%m-%d %H:%M")
echo "${hour}  执行主机拨号" | tee -a $file


function check_ppp()
{
	pppd_num=`/usr/bin/ps aux|grep "pppd call ppp${i} "|grep -v "grep"|wc -l`
	if [ $pppd_num -gt 0 ];then
		echo "开始拨号"
		exit
	fi
}

function check_pppoe_config()
{
	if [[ ! -s "/etc/rsyslog.d/pppd.conf" ]];then
		curl -sSL http://down.9494.org:9999/pppoe/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
		pip3 install natsort
	fi
	if [[ ! -s "/opt/pppoe/route.txt" ]];then
	    mgt_gateway=`/usr/sbin/ip route | grep default | cut -d ' ' -f 3 | head -1`
		mgt_dev=`/usr/sbin/ip route | grep default | cut -d ' ' -f 5 | head -1`
		mgt_ip=`/usr/sbin/ip addr show dev $mgt_dev | grep -w "inet" | cut -d ' ' -f 2 | cut -d ' ' -f 1`
		mgt_net=`/usr/sbin/ip route | grep -w $mgt_dev | grep -w $mgt_ip | cut -d ' ' -f 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
	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
unit=0
EOF
cat  > /root/ppp0 << EOF
plugin rp-pppoe.so macv00
user "1234567"
persist
maxfail 0
holdoff 20
unit 0
linkname ppp0
EOF
	systemctl stop firewalld
	systemctl disable firewalld
	if [[ -s "/opt/pppoe/route.txt" ]];then
		mgt_gateway=`cat /opt/pppoe/route.txt|sed -n 1p|cut -d ' ' -f 1`
		mgt_dev=`cat /opt/pppoe/route.txt|sed -n 2p|cut -d ' ' -f 1`
		mgt_ip=`cat /opt/pppoe/route.txt|sed -n 3p|cut -d ' ' -f 1`
		mgt_net=`cat /opt/pppoe/route.txt|sed -n 4p|cut -d ' ' -f 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 http://down.9494.org:9999/pppoe/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
		sed -i "s/unit=0/unit=${i}/g" /etc/sysconfig/network-scripts/ifcfg-ppp${i}
		cp /root/ppp0 /etc/ppp/peers/ppp${i}
		sed -i "s/1234567/${user}/g" /etc/ppp/peers/ppp${i}
		sed -i "s/macv00/${pppoe_eth}/g" /etc/ppp/peers/ppp${i}
		sed -i "s/unit 0/unit ${i}/g" /etc/ppp/peers/ppp${i}
		sed -i "s/linkname ppp0/linkname ppp${i}/g" /etc/ppp/peers/ppp${i}
	done
	for i in `seq 0 $(($num-1))`
	{
		sleep 10
		pppd call ppp${i} &
	}
	wait
	for i in `seq 0 $(($num-1))`
	{
		#pppd call ppp${i}
		pppoe_status=`ip add | grep inet | grep peer |grep ppp${i}|wc -l`
		#删除现有路由表
		ip rule del lookup ppp${i}
		#1,删除现有自定义路由表
		sed -i "/ppp${i}/d" /etc/iproute2/rt_tables
		#2,添加自定义路由表
		echo "$(($i+10))   ppp${i}" >> /etc/iproute2/rt_tables
		echo $pppoe_status
		if [[ $pppoe_status -gt 0 ]];then
			echo "${dev_name} 拨号成功"
			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=`ip address | grep inet | grep peer |grep ppp${i} | awk '{print $2}'`
			ip route flush table ppp${i}
			ip route add 0/0 dev ${dev_name} table ppp${i}
			route_num=`ip add | grep inet | grep peer |grep ppp${i}|wc -l`
			if [[ $route_num==1 ]];then
				echo "${dev_name} add success"
			fi
			rulenum=`/usr/sbin/ip rule |grep ppp${i} | cut -d ' ' -f 5 |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
		else
			echo "${dev_name} 拨号失败"
		fi
	}
}

function pppoe_crontab()
{
	curl -o /opt/pppoe/ppp_check.sh http://down.9494.org:9999/pppoe/ppp_check.sh
	curl -o /opt/pppoe/ppp_rule_check.sh http://down.9494.org:9999/pppoe/ppp_rule_check.sh
	curl -o /opt/pppoe/ppp.sh http://down.9494.org:9999/pppoe/ppp.sh
	chmod +x /opt/pppoe/ppp_check.sh
	chmod +x /opt/pppoe/ppp_rule_check.sh
	chmod +x /opt/pppoe/ppp.sh

	
	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 "/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 "/ppp.sh/d" /etc/rc.local
	echo "sh /opt/pppoe/ppp.sh" >> /etc/rc.local
	chmod +x /etc/rc.d/rc.local
}

pppoe_crontab
check_ppp
check_pppoe_config
create_vlan