Add multiple interface and IP address – ubuntu and centos

The below script uses to add bulk IPs for the same gateway and subnet :

Save IPs in a file like /opt/dummy.txt and run the script to add them up.

Interface add script – Ubuntu or Centos:

echo Enter Distribution ubuntu\/centos
read os
if [ "$os" == "centos" ]
then
echo Enter Interface Name
read intface
echo Enter IPdetails File name with PATH
read pat
echo Enter Netmask
read Net
echo Enter Gateway
read Gtw

alas=`cat $pat |wc -l`
for (( i=1 ; i<=$alas ; i++ ))
do
ipl=`head -n $i $pat |tail -n 1`
Ips=`echo $ipl|cut -d" " -f1`
#Net=`echo $ipl|cut -d" " -f2`
#Gtw=`echo $ipl|cut -d" " -f3`
grep -rw $Ips /etc/sysconfig/network-scripts>/opt/dummy.txt
if [[ "$?" -ne "0" ]]
then
alaupdate=`ls /etc/sysconfig/network-scripts|grep ifcfg-$intface|grep :|cut -d":" -f2|sort -n|tail -n 1`
alaactual=`expr $alaupdate + 1`

echo DEVICE=$intface:$alaactual >/etc/sysconfig/network-scripts/ifcfg-$intface:$alaactual
echo BOOTPROTO=none >>/etc/sysconfig/network-scripts/ifcfg-$intface:$alaactual
echo IPADDR=$Ips>>/etc/sysconfig/network-scripts/ifcfg-$intface:$alaactual
echo NETMASK=$Net>>/etc/sysconfig/network-scripts/ifcfg-$intface:$alaactual
echo ONBOOT=yes>>/etc/sysconfig/network-scripts/ifcfg-$intface:$alaactual
echo GATEWAY=$Gtw>>/etc/sysconfig/network-scripts/ifcfg-$intface:$alaactual
echo TYPE=Ethernet>>/etc/sysconfig/network-scripts/ifcfg-$intface:$alaactual
else
echo The $Ips Already exist in one Interface
fi
done
else
if [ "$os" == "ubuntu" ]
then
echo Enter Interface Name
read intface
echo Enter IPdetails File name with PATH
read pat
echo Enter Netmask
read Net
echo Enter Gateway
read Gtw
alas=`cat $pat |wc -l`
for (( i=1 ; i<=$alas ; i++ ))
do
ipl=`head -n $i $pat |tail -n 1`
Ips=`echo $ipl|cut -d" " -f1`
#Net=`echo $ipl|cut -d" " -f2`
#Gtw=`echo $ipl|cut -d" " -f3`
cat /etc/network/interface|grep -w $Ips
if [[ "$?" -ne "0" ]]
then
alaupdate=`cat /etc/network/interface |grep auto|wc -l`
alaactual=`expr $alaupdate + 1`
echo >>/etc/network/interface
echo auto $intface:$alaactual >>/etc/network/interface
echo iface $intface:$alaactual inet static >>/etc/network/interface
echo address $Ips >>/etc/network/interface
echo netmask $Net >>/etc/network/interface
echo gateway $Gtw >>/etc/network/interface
else
echo $Ips is already exist in file
fi
done
else
echo only ubuntu or centos allowed
fi
fi

The below script will use to add bulk IPs in different gateway and subnet:
( Save the Ips in a file like IP MASK GATEWAY .For Example 192.168.1.2 255.255.255.0 192.168.1.1 )

echo Enter Distribution ubuntu\/centos
read os
if [ "$os" == "centos" ]
then
echo Enter Interface Name
read intface
echo Enter IPdetails File name with PATH
read pat
alas=`cat $pat |wc -l`
for (( i=1 ; i<=$alas ; i++ ))
do
ipl=`head -n $i $pat |tail -n 1`
Ips=`echo $ipl|cut -d" " -f1`
Net=`echo $ipl|cut -d" " -f2`
Gtw=`echo $ipl|cut -d" " -f3`
grep -rw $Ips /etc/sysconfig/network-scripts>/opt/dummy.txt
if [[ "$?" -ne "0" ]]
then
alaupdate=`ls /etc/sysconfig/network-scripts|grep ifcfg-$intface|grep :|cut -d":" -f2|sort -n|tail -n 1`
alaactual=`expr $alaupdate + 1`
echo DEVICE=$intface:$alaactual >/etc/sysconfig/network-scripts/ifcfg-$intface:$alaactual
echo BOOTPROTO=none >>/etc/sysconfig/network-scripts/ifcfg-$intface:$alaactual
echo IPADDR=$Ips>>/etc/sysconfig/network-scripts/ifcfg-$intface:$alaactual
echo NETMASK=$Net>>/etc/sysconfig/network-scripts/ifcfg-$intface:$alaactual
echo ONBOOT=yes>>/etc/sysconfig/network-scripts/ifcfg-$intface:$alaactual
echo GATEWAY=$Gtw>>/etc/sysconfig/network-scripts/ifcfg-$intface:$alaactual
echo TYPE=Ethernet>>/etc/sysconfig/network-scripts/ifcfg-$intface:$alaactual
else
echo The $Ips Already exist in one Interface
fi
done
else
if [ "$os" == "ubuntu" ]
then
echo Enter Interface Name
read intface
echo Enter IPdetails File name with PATH
read pat
alas=`cat $pat |wc -l`
for (( i=1 ; i<=$alas ; i++ ))
do
ipl=`head -n $i $pat |tail -n 1`
Ips=`echo $ipl|cut -d" " -f1`
Net=`echo $ipl|cut -d" " -f2`
Gtw=`echo $ipl|cut -d" " -f3`
cat /etc/network/interface|grep -w $Ips
if [[ "$?" -ne "0" ]]
then
alaupdate=`cat /etc/network/interface |grep auto|wc -l`
alaactual=`expr $alaupdate + 1`
echo >>/etc/network/interface
echo auto $intface:$alaactual >>/etc/network/interface
echo iface $intface:$alaactual inet static >>/etc/network/interface
echo address $Ips >>/etc/network/interface
echo netmask $Net >>/etc/network/interface
echo gateway $Gtw >>/etc/network/interface
else
echo $Ips is already exist in file
fi
done
else
echo only ubuntu or centos allowed
fi
fi