#!/bin/bash
function creat_vlan_eth()
{
	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
	if [ ! -s /usr/sbin/ifdown-macvlan ];then 
		curl -o /usr/sbin/ifdown-macvlan http://down.9494.org:9999/pppoe/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/pppoe/ifup-macvlan && chmod +x /usr/sbin/ifup-macvlan
	fi
	num=`cat /opt/pppoe/pppoe_user.txt |wc -l`
	for i in `seq 0 $(($num-1))`
	do
		vlanid=`cat /opt/pppoe/pppoe_user.txt|sed -n $(($i+1))p|awk '{print $1}'`
		p_eth=`cat /opt/pppoe/pppoe_user.txt|sed -n $(($i+1))p|awk '{print $4}'`
		if [[ $vlanid == "novlan" ]] ;then
			eth_name=`printf "macv%.2d" $i`
			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}
				chattr +i /etc/sysconfig/network-scripts/ifcfg-${eth_name}
				/usr/sbin/ifup-macvlan ${eth_name} &
			else
				macv_num=`/usr/sbin/ip addr|grep ${eth_name}@|wc -l`
				if [[ $macv_num -le 0 ]];then
					chattr -i /etc/sysconfig/network-scripts/ifcfg-${eth_name}
					sed -i '/MACVLAN_PARENT/d' /etc/sysconfig/network-scripts/ifcfg-${eth_name}
					echo "MACVLAN_PARENT=${p_eth}" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
					chattr +i /etc/sysconfig/network-scripts/ifcfg-${eth_name}
					/usr/sbin/ifup-macvlan ${eth_name} &
				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}
				eth_name=`printf "macv%.2d" $i`
				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}
					chattr +i /etc/sysconfig/network-scripts/ifcfg-${eth_name}
					/usr/sbin/ifup-macvlan ${eth_name} &
				else
					macv_num=`/usr/sbin/ip addr|grep ${eth_name}@|wc -l`
					if [[ $macv_num -le 0 ]];then
						#echo"修正vlanid ${eth_name}"
						chattr -i /etc/sysconfig/network-scripts/ifcfg-${eth_name}
						sed -i '/MACVLAN_PARENT/d' /etc/sysconfig/network-scripts/ifcfg-${eth_name}
						echo "MACVLAN_PARENT=${p_eth}.${vlanid}" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
						chattr +i /etc/sysconfig/network-scripts/ifcfg-${eth_name}
						/usr/sbin/ifup-macvlan ${eth_name} &
					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}
				fi
				eth_name=`printf "macv%.2d" $i`
				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}
					chattr +i /etc/sysconfig/network-scripts/ifcfg-${eth_name}
					/usr/sbin/ifup-macvlan ${eth_name} &
				else
					macv_num=`/usr/sbin/ip addr|grep ${eth_name}@|wc -l`
					if [[ $macv_num -le 0 ]];then
						#echo"修正vlanid ${eth_name}"	
						chattr -i /etc/sysconfig/network-scripts/ifcfg-${eth_name}
						sed -i '/MACVLAN_PARENT/d' /etc/sysconfig/network-scripts/ifcfg-${eth_name}
						echo "MACVLAN_PARENT=${p_eth}.${vlanid}" >> /etc/sysconfig/network-scripts/ifcfg-${eth_name}
						chattr +i /etc/sysconfig/network-scripts/ifcfg-${eth_name}
						/usr/sbin/ifup-macvlan ${eth_name} &
					fi
				fi
			fi
		fi	
	done
}

creat_vlan_eth
wait