站群服务器有很多IP地址,如果手动配置,需要很多时间。用shell脚本来实现自动配置能有效提高工作效率。如下:
- 先给服务器配置好第一个IP,然后ssh登录服务器;
- 在/root下创建文件iprange.txt,并将IP段写入iprange.txt;如下
#cat iprange.txt
192.168.10.0/28
192.168.10.16/28
192.168.20.32/28
........
- 创建脚本
#vi iprange.sh
#!/bash/bash
read -p "please enter ip_range‘txt document : " iprange.txt
PATH=/etc/sysconfig/network-scripts
N=0
S=0
for i in `/bin/cat /root/iprange.txt`
do
ID=${i#*/} #截取后缀
ADD=${i%.*} #截取网段
PREFIX=${i%/*}
HOST=${PREFIX##*.} #截取第4个字段
if [ $ID -eq 28 ] ;then
START=`/usr/bin/expr $HOST + 2`
END=`/usr/bin/expr $START + 12`
M=240
if [ $N -eq 0 ] ; then
S=0
else
let S+=`/usr/bin/expr $END - $START + 1`
fi
fi
if [ $ID -eq 24 ] ;then
START=`/usr/bin/expr $HOST + 2`
END=`/usr/bin/expr $START + 252`
M=0
if [ $N -eq 0 ] ; then
S=0
else
let S+=`/usr/bin/expr $END - $START + 1`
fi
fi
if [ $ID -eq 25 ] ;then
START=`/usr/bin/expr $HOST + 2`
END=`/usr/bin/expr $START + 124`
M=128
if [ $N -eq 0 ] ; then
S=0
else
let S+=`/usr/bin/expr $END - $START + 1`
fi
fi
if [ $ID -eq 26 ] ;then
START=`/usr/bin/expr $HOST + 2`
END=`/usr/bin/expr $START + 60`
M=192
if [ $N -eq 0 ] ; then
S=0
else
let S+=`/usr/bin/expr $END - $START + 1`
fi
fi
if [ $ID -eq 27 ] ;then
START=`/usr/bin/expr $HOST + 2`
END=`/usr/bin/expr $START + 28`
M=224
if [ $N -eq 0 ] ; then
S=0
else
let S+=`/usr/bin/expr $END - $START + 1`
fi
fi
if [ $ID -eq 29 ] ;then
START=`/usr/bin/expr $HOST + 2`
END=`/usr/bin/expr $START + 4`
M=248
if [ $N -eq 0 ] ; then
S=0
else
let S+=`/usr/bin/expr $END - $START + 1`
fi
fi
echo -n -e "DEVICE=eth$j\nONBOOT=yes\nBOOTPROTO=static\nIPADDR_START=${ADD}.$START\nIPADDR_END=${ADD}.$END\nCLONENUM_START=$S\nNETMASK=255.255.255.$M" >$PATH/ifcfg-eth0-range$N
let N++
done
4.执行脚本
#sh iprange.sh
5.重启网卡并检查是否配置成功
#service network restart
#ifconfig
6.删除脚本
完毕!