经常在linux下做网络实验的朋友,经常要反复更改ip,虽说GUI界面简化了工作量,但常常也需忙的不亦乐乎。最近写了个更改网卡ip的脚本,有不足之处,欢迎大家指正。

 

#!/bin/bash

echo "This will help you to change the configure of eth0!"

FILE_NAME='/etc/sysconfig/network-scripts/ifcfg-eth0'

sed -i 's/^\([[:space:]]*BOOTPROTO=\).*$/\1static/g' $FILE_NAME

read -p "Please input a right ip address:" IP_ADDR

echo $IP_ADDR | grep -E '([0-9]+(\.[0-9]+){3})' &> /dev/null

until [ $? -eq 0 ] ;do

read -p "Please input a right ip address:" IP_ADDR

        echo $IP_ADDR | grep -E '([0-9]+(\.[0-9]+){3})' &> /dev/null

done

sed -i '/IPADDR=.*/'d $FILE_NAME

echo "IPADDR=$IP_ADDR >> $FILE_NAME

read -p "Please input a right netmask:" NET_MASK

echo $NET_MASK | grep -E '([0-9]+(\.[0-9]+){3})' &> /dev/null

until [ $? -eq 0 ] ;do

        read -p "Please input a right netmask:" NET_MASK

        echo $NET_MASK | grep -E '([0-9]+(\.[0-9]+){3})' &> /dev/null

done

sed -i '/NETMASK=.*/'d $FILE_NAME

echo "NETMASK=$NET_MASK >> $FILE_NAME