#!/bin/bash
function check_pppoe_config()
{
	config_list=`ls /etc/sysconfig/network-scripts/ifcfg-ppp*|grep "/etc/sysconfig/network-scripts/ifcfg-ppp[1-9]*[0-9]*$"|sort -Vk 1`
	echo "" > /opt/pppoe/pppoecfg.txt
	for config in $config_list
	do
		clo=`echo $config | sed "s/\/etc\/sysconfig\/network-scripts\/ifcfg-ppp//g"`
		#ip=`/usr/sbin/ifconfig ppp$clo|awk '$1~"inet" && $2~"^[0-9]"{print $2 }'`
		#gw=`/usr/sbin/ip addr|grep -A 3 ppp${clo}:|awk  '$1~"inet" && $2~"^[0-9]"{print $4}'|awk -F '/' '{printf $2}'`
		pppoe_status_1=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-ppp${clo}|grep "Link is up"|wc -l`
		if [[ $pppoe_status_1 -gt 0 ]];then
			ip=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-ppp${clo}|awk '$1~"inet$" && $2~"^[0-9]"{print $2 }'`
			gw=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-ppp${clo}|awk  '$1~"inet$" && $2~"^[0-9]"{print $4}'|awk -F '/' '{printf $2}'`
			dev_name=`/usr/sbin/pppoe-status /etc/sysconfig/network-scripts/ifcfg-ppp${clo}|awk '$1~"^[0-9]" && $2~"ppp"{print $2}'|sed 's/://g'`
			bandwith=`cat /opt/pppoe/pppoe_user.txt|sed -n $(($i+1))p|awk '{print $5}'`
			echo "${dev_name} ${ip}/${gw} ${bandwith}000000" >> /opt/pppoe/pppoecfg.txt
		fi
	done
	sed -i '/^$/d' /opt/pppoe/pppoecfg.txt
	if  [[ ! -f  "/opt/hyops/provider/provider_info.txt" ]];then
		mkdir -p /opt/hyops/provider
	fi
	cols_num1=`cat /opt/pppoe/pppoecfg.txt | awk '{print NF}'`
	line_num1=1
	for col1 in $cols_num1
	do
		if [[ $col1 -ne 3 ]];then
			echo "$line_num1 line /opt/pppoe/pppoecfg.txt information error"
			exit
		fi
		let line_num1=line_num1+1
	done
	if  [[ ! -f  "/opt/pppoe/pppoe_json.py" ]];then
		curl -sSL -o /opt/pppoe/pppoe_json.py http://down.994.org:9999/pppoe/pppoe_json.py
	fi
	python /opt/pppoe/pppoe_json.py
	
}

check_pppoe_config