#!/bash/bash
read -p "please enter ip_range'txt document : " TXT
File=`pwd`/$TXT
if [ ! -e $File  ]; then
echo -e "\\033[31mThis document is not exsit!!!\\033[0m"
exit
fi

read -p "Please enter vaild network interface [eth0]:  " NET
if [ -z $NET  ]; then
NET=eth0
fi

PATH=/etc/sysconfig/network-scripts
DEL=$PATH/ifcfg-*-range*
/bin/rm -rf $DEL
N=0
S=0
for i in `/bin/cat $File`
do
ID=${i#*/}
ADD=${i%.*}
PREFIX=${i%/*}
HOST=${PREFIX##*.}
if  [ $ID -eq 28 ] ;then
        if [ $N -eq 0  ] ; then
                S=0
        else
                let S+=`/usr/bin/expr $END - $START + 1`
        fi
START=`/usr/bin/expr $HOST + 2`
END=`/usr/bin/expr $START + 12`
M=240
fi
if  [ $ID -eq 24 ] ;then
        if [ $N -eq 0  ] ; then
        S=0
        else
                let S+=`/usr/bin/expr $END - $START + 1`
        fi
START=`/usr/bin/expr $HOST + 2`
END=`/usr/bin/expr $START + 252`
M=0
fi
if  [ $ID -eq 25 ] ;then
        if [ $N -eq 0  ] ; then
                S=0
        else
                let S+=`/usr/bin/expr $END - $START + 1`
        fi
START=`/usr/bin/expr $HOST + 2`
END=`/usr/bin/expr $START + 124`
M=128
fi
if  [ $ID -eq 26 ] ;then
        if [ $N -eq 0  ] ; then
                S=0
        else
                let S+=`/usr/bin/expr $END - $START + 1`
        fi
START=`/usr/bin/expr $HOST + 2`
END=`/usr/bin/expr $START + 60`
M=192
fi
if  [ $ID -eq 27 ] ;then
        if [ $N -eq 0  ] ; then
                S=0
        else
                let S+=`/usr/bin/expr $END - $START + 1`
        fi
START=`/usr/bin/expr $HOST + 2`
END=`/usr/bin/expr $START + 28`
M=224
fi
if  [ $ID -eq 29 ] ;then
        if [ $N -eq 0  ] ; then
                S=0
        else
                let S+=`/usr/bin/expr $END - $START + 1`
        fi
START=`/usr/bin/expr $HOST + 2`
END=`/usr/bin/expr $START + 4`
M=248
fi
echo -n -e "DEVICE=${NET}\nONBOOT=yes\nBOOTPROTO=static\nIPADDR_START=${ADD}.$START\nIPADDR_END=${ADD}.$END\nCLONENUM_START=$S\nNETMASK=255.255.255.$M\nARPCHECK=no" > $PATH/ifcfg-${NET}-range$N
let N++
done
echo -e "\\033[32mPlease restart network with 'service network restart'\\033[0m"
echo -e "++++++++++++++++++++\\033[32mauthor sandy.data99\\033[0m+++++++++++++"
sed -i "s/\\\\|ifcfg-\.\*-range//g" /etc/init.d/network
systemctl daemon-reload
systemctl restart network
