Centos批量绑定IPV6地址

作者: ropon 分类: Shell 发布时间: 2019-02-21 15:10
#!/bin/bash
# Author: Ropon
# Blog: https://www.ropon.top

eth0f="/etc/sysconfig/network-scripts/ifcfg-eth0"
resf="/etc/resolv.conf"
ipv6=""
ipgw=""
restartflag=0

Help() {
	echo "Usage: ./$(basename $0) -s[-b] ipv6 [ipv6]"
	echo
	echo "OPTIONS:"
	echo "-s | --single: Binding IPV6"
	echo "-b | --batch: Batch Binding IPV6"
	echo
	echo "Example: ./$(basename $0) -s 240e:d9:c200:101:7bb2::120"
	echo "Example: ./$(basename $0) -b 240e:d9:c200:101:7bb2::120 240e:d9:c200:101:7bb2::130"
	echo
}

CheckIpv6() {
	echo "ipv6"
}

Batch() {
	start="`echo $1|awk -F ':' '{print $NF}'`"
	end="`echo $2|awk -F ':' '{print $NF}'`"
	pre=`echo ${1%:*}`
	dstart=`printf %d 0X${start}`
	dend=`printf %d 0X${end}`
	total=$(($dend-$dstart+1))
	ipv6string=""
	for ((i=1;i<=$total;i++))
	do	
		[ $i -eq $total ] && ipv6string=${ipv6string}${pre}:`printf %x $dstart`"/64" || ipv6string=${ipv6string}${pre}:`printf %x $dstart`"/64 \\ "
		let dstart=dstart+1	
	done
	echo "Batch Binding IPV6"
	echo "IPV6ADDR_SECONDARIES=\"$ipv6string\"" >> $eth0f
	SetIpv6 $1 "-b"
}

SetIpv6() {
	ipv6=$1
	ipgw=`echo $ipv6|awk -F ':' '{print $1":"$2":"$3":"$4"::1"}'`
	[ -f $eth0f ] && cp $eth0f{,.bak}
	if [ -z "`grep ^'IPV6INIT=yes' $eth0f`" ]; then
		echo "IPV6INIT=yes" >> $eth0f
		echo "IPV6_DEFROUTE=yes" >> $eth0f
		[ -z $2 ] && echo "IPV6ADDR=${ipv6}/64" >> $eth0f
		echo "IPV6_DEFAULTGW=$ipgw" >> $eth0f
		restartflag=1
	else
		echo "skip set ipv6"
	fi
	[ -f $resf ] && cp $resf{,.bak}	
	if [ -z "`grep ^'nameserver 240e:56:4000:8000::69' $resf`" ]; then
		echo "nameserver 240e:56:4000:8000::69" >> $resf
		echo "nameserver 240C::6666" >> $resf
		restartflag=1
	else
		echo "skip set ipv6 dns"
	fi
	[ $restartflag -eq 1 ] && service network restart && ifconfig|grep inet6|grep global|awk '{print $2}' && echo "set ipv6 success"
}

[ -z $1 ] && ifconfig|grep inet6|grep global|awk '{print $2}'
while [ $1 ]; do
	case $1 in
		'-b' | '--batch' )
			[ -z "$2" -o -z "$3" ] && echo "Incomplete parameters" && exit
			Batch $2 $3
			break
			;;
		'-s' | '--single' )
			[ -z "$2" ] && echo "Incomplete parameters" && exit
			SetIpv6 $2
			break
			;;
		'-h' | '--help' )
			Help
			break
			;;
		* )
			Help
			exit
			;;
	esac
	shift		
done

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!