shell更改网卡ip地址

#!/bin/sh

wk=$(ifconfig -a |grep eth|awk '{print $1}')

wks=$(ifconfig -a |grep eth|awk '{print $1}'|wc -l)

echo "网卡总数是:$wks(个)"

net_path="/etc/sysconfig/network-scripts"

time=$(date -d "today" +"%Y%m%d%H%M%S")

cd $net_path

for w in $wk;do

        info=$(mii-tool $w)

        #echo "$info"

        info1=$(mii-tool $w | grep link)

        echo "$info1"

file="ifcfg-$w"

echo "是否设置${w}网卡?(y|n)"

read wksz

if [ $wksz = "y" ];then

if [ -f "$file" ];then

               mv $file /home/$file.bak$time

        fi

cat /dev/null > $file

echo DEVICE="${w}" >> $file

echo "ONBOOT=yes" >> $file

read -p "输入${w} IP地址:" ip

echo IPADDR="${ip}" >> $file

read -p "输入子网掩码:" mask

echo NETMASK="${mask}" >> $file

read -p "输入默认网关:" gw

echo GATEWAY="${gw}" >> $file

read -p "输入主DNS:" dns

echo DNS1="${dns}" >> $file

elif [ $wksz = "n" ];then

continue

else

echo "错误输入!"

exit 0

fi

done

/etc/init.d/network restart

相关推荐