公司三台服务器分别要绑定15个虚拟IP地址,所以写了以下脚本来实现.
ip---> 为start_ip或end_ip地址前三段(需相同).
start_num---> 为start_ip最后一段
end_num---> 为end_ip最后一段
#/bin/bash
path=/etc/sysconfig/network-scripts/
cd $path
read -p "input start_ip:" start_ip
read -p "input end_ip:" end_ip
ip=`echo $start_ip |awk -F"." '{print $1"."$2"."$3"."}'`
start_num=`echo $start_ip |awk -F"." '{print $NF}'`
end_num=`echo $end_ip |awk -F"." '{print $NF}'`
k=$end_num
for ((i = $start_num ; i <= $end_num ; i++ ))
do
n=`expr $end_num - $k + 1`
cp ifcfg-eth0:0 ifcfg-eth0:$n
sed -i 's/eth0:0/eth0:'"$n"'/g' ifcfg-eth0:$n
sed -i '5,5s/^.*$/IPADDR='"$ip$i"'/g' ifcfg-eth0:$n
k=`expr $k - 1`
done
cat ifcfg-eth0:0
DEVICE="eth0:0"
BOOTPROTO=none
ONBOOT=yes
TYPE="Ethernet"
IPADDR=192.168.1.100
NETMASK=255.255.255.255
NAME="System eth0:0"
转载于:https://blog.51cto.com/linuxart/1220413