centos6添加整段多IP脚本

#这个脚本开始前会自动删除range文件
#IP段与IP段之间可以同行但是必须用分号或空格隔开
#可以有空行

#!/bin/bash
eth_file=(`ls /etc/sysconfig/network-scripts | grep "ifcfg-*"`)
select eths in "${eth_file[@]}";do
if [[ -z $eths ]];then
echo "please to select "
continue
fi
eth_name=${eths#*-}
echo "your select is $eth_name"
break
done

function function_ipadd (){
mask=${3##*/}
ipall=${3%%/*}
ip_b=${ipall##*.}
ip_f=${ipall%.*}
masks=`function_masks $mask`
ipsnu=$?
let ips_start=$ip_b+2
let ips_end=$ips_start+$ipsnu-1
ethname="ifcfg-$1-range$2"
echo -e "DEVICE=$1 BOOTPROTO=static CLONENUM_START=$4 IPADDR_START=$ip_f.$ips_start IPADDR_END=$ip_f.$ips_end NETMASK=$masks ONBOOT=yes" > /etc/sysconfig/network-scripts/$ethname
let cont=$ipsnu+$4
return $cont
}
function function_masks(){
if [[ $1 -eq 24 ]] ;then echo "255.255.255.0";return 253;fi
if [[ $1 -eq 26 ]] ;then echo "255.255.255.192";return 61;fi
if [[ $1 -eq 27 ]] ;then echo "255.255.255.224";return 29;fi
if [[ $1 -eq 28 ]] ;then echo "255.255.255.240";return 13;fi
if [[ $1 -eq 29 ]] ;then echo "255.255.255.248";return 5;fi

}
tmp_file="ipadd_$(date +%Y%m%d%H%M%S).txt"
aaa=`ls /etc/sysconfig/network-scripts | grep ifcfg-*-range*`
if [[ -n $aaa ]];then
rm -rf /etc/sysconfig/network-scripts/ifcfg-*range*
fi
vi /tmp/$tmp_file
if [[ -n /tmp/$tmp_file ]];then
nu=0
i=0
cat /tmp/$tmp_file | egrep -o "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/[0-9]+" | while read line
do
function_ipadd $eth_name $i $line $nu
nu=$?
let i+=1
rm -rf /tmp/$tmp_file
echo "Please restart the network....."
done
else
echo "error.......................... "
exit
fi

原文地址:https://www.cnblogs.com/uyahs/p/7670399.html