前提條件是所有網卡未配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