前提条件是所有网卡未配IP,运行前需要根据现实情况中的网关IP和网卡数对脚本进行修改。
此脚本网关为192.168.xx.1 网卡数为8
脚本内容
#!/bin/sh
Gatewayip=1
ethnum=8
ipnum=($@)
for (( n=0;n<=$ethnum-1; n++ )); do
for (( i=1;i<=$#-1; i++ ));do
if [ "${ipnum[$i]}" == "" ]; then
continue
fi
ifconfig eth$n 192.168.${ipnum[$i]}.$1 netmask 255.255.255.0
tong=`ping -c 1 192.168.${ipnum[$i]}.$Gatewayip | grep ttl`
echo "now eth$n is 192.168.${ipnum[$i]}.$1"
if [ ! -z "$tong" ];then
echo "the correct ip of eth$n is 192.168.${ipnum[$i]}.$1"
unset ipnum[$i]
break
fi
if [ -z "$tong" ];then
ip addr del 192.168.${ipnum[$i]}.$1/24 dev eth$n
fi
done
done
脚本使用方法 file.sh 100 1 2 3 4 5 6 脚本会为8个网卡找出正确的IP并配上192.168.1.100 192.168.2.100 192.168.3.100 192.168.4.100 192.168.5.100 192.168.6.100
网关为 192.168.1.1 192.168.2.1 192.168.3.1 192.168.4.1 192.168.5.1 192.168.6.1