#!/bin/bash
# 单网卡配置多ip脚本


systemctl stop NetworkManager > /dev/null 2>&1
systemctl disable NetworkManager > /dev/null 2>&1
systemctl start network > /dev/null 2>&1
systemctl enable network > /dev/null 2>&1


rpm -qa|grep "wget" &> /dev/null
if [ $? == 0 ]; then
    echo 亲多喝水对身体有好处哦！！
else
    yum -y install wget
fi


rm -f $0

# 改为实际的子网掩码
mask=255.255.255.0

wget -O /root/ip.txt  http://laiyihan.tpddns.cn:8001/VPS/SK5/ip2.txt


if [ ! -z $1 ]; then
	mask=$1
fi

if [ ! -e /usr/bin/mkpasswd ];then
	yum install -y expect
fi

if [ ! -e /usr/bin/dos2unix ];then
	yum install -y dos2unix
fi

if [ ! -e /usr/bin/expr ];then
	yum install -y coreutils
fi

if [ ! -e ./ip2.txt ];then
	echo "ip.txt 文件不存在，自行创建。"
	echo "将待添加的ip加入到 ip.txt 文件中，一行一个。"
	exit
fi

dos2unix ip2.txt

if [ ! -e /etc/sysconfig/network-scripts/ifcfg-eth0 ]; then
	echo "eth0 主网卡配置文件不存在。"
	exit
fi

start=0

while read line || [[ -n ${line} ]]
do

echo "创建 $line 网卡配置文件 ifcfg-eth0:$start"
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-eth0:$start
DEVICE=eth0:$start
BOOTPROTO=static
ONBOOT=yes
IPADDR=$line
NETMASK=$mask
EOF

start=`expr $start + 1`

done < ip2.txt

echo "重启网络服务中，如果重启后看到的ip数量和绑定数量不一致，可再次尝试重启网络。"
systemctl restart network
echo ""

ip -4 a | grep inet | grep eth0