linux所有網卡的配置文件都保存在 /etc/sysconfig/network-scripts 目錄中,一個網卡對應一個配置文件。只需複製一份,修改文件名,然後修改其中的DEVICE和IPADDR字段就可以。配置完成後重啓網卡(命令:service network restart)即可。
#! /bin/sh
#批量添加IP腳本
#網絡文件 填寫網卡路徑
networkfile="/etc/sysconfig/network-scripts/ifcfg-eth0"
#網卡名字
ethname="eth0"
#網卡開始的編號
virtul_eth=1
#添加的IP數量
ipcount=100
#要添加的ip網段
baseaddress="192.168.20"
#從baseip開始掃描IP地址 控制IP開始的地址 baseaddress.baseip
baseip="100"
#eth0網卡添加192.168.20.100 -- 192.168.20.199
while [ 1 ]
do
if [ $baseip -gt 254 -o $ipcount -eq 0 ];then
break
fi
ping -c2 ${baseaddress}.${baseip}
if [ "x$?" != "x0" ]
then
echo "IP:[${baseaddress}.${baseip}] not be used yet!" >> iplist.txt
cp ${networkfile} ${networkfile}:${virtul_eth}
grep -E -v "DEVICE|IPADDR" ${networkfile}:${virtul_eth} > 1.txt
echo "DEVICE=$ethname:$virtul_eth" >> 1.txt
echo "IPADDR=${baseaddress}.${baseip}" >> 1.txt
mv -f 1.txt ${networkfile}:${virtul_eth}
else
echo "Bad IP[${baseaddress}.${baseip}]"
fi
baseip=$(expr ${baseip} + 1)
virtul_eth=$(expr ${virtul_eth} + 1)
ipcount=$(expr ${ipcount} - 1)
done
#添加完 重啓所有網卡
service network restart