#!/bin/bash # MY_ONBOOT="ONBOOT=yes" MY_BOOTPROTO="BOOTPROTO=static" MY_DEVICE="DEVICE="`ifconfig -a|grep HWaddr|awk {‘print $1‘}` MY_HWADDR="HWADDR="`ifconfig -a|grep HWaddr|awk {‘print $5‘}` MY_IPADDR="IPADDR="$1 MY_NETMASK="NETMASK="$2 MY_GATEWAY="GATEWAY="$3 MY_DNS="DNS1="$4 Flag1=0 Flag2=0 Flag3=0 Flag4=0 if [ $# -lt 4 ];then echo "Usage:./modif_ip.sh arg1[IPADDR] arg2[NETMASK] arg3[GATEWAY] arg4[DNS]." exit 5 fi if [ $# -gt 4 ];then echo "Usage:./modif_ip.sh arg1[IPADDR] arg2[NETMASK] arg3[GATEWAY] arg4[DNS]." exit 6 fi cat /etc/sysconfig/network-scripts/ifcfg-eth0 | while read myline do if [[ "$myline" =~ "ONBOOT" ]];then echo $MY_ONBOOT >> ifcfg-eth0 elif [[ "$myline" =~ "BOOTPROTO" ]];then echo $MY_BOOTPROTO >> ifcfg-eth0 elif [[ "$myline" =~ "DEVICE" ]];then echo $MY_DEVICE >> ifcfg-eth0 elif [[ "$myline" =~ "HWADDR" ]];then echo $MY_HWADDR >> ifcfg-eth0 elif [[ "$myline" =~ "IPADDR" ]];then Flag1=1 elif [[ "$myline" =~ "NETMASK" ]];then Flag2=1 elif [[ "$myline" =~ "GATEWAY" ]];then Flag3=1 elif [[ "$myline" =~ "DNS" ]];then Flag4=1 else echo $myline >> ifcfg-eth0 fi done if [ $Flag1 == 0 ];then echo $MY_IPADDR >> ifcfg-eth0 fi if [ $Flag2 == 0 ];then echo $MY_NETMASK >> ifcfg-eth0 fi if [ $Flag3 == 0 ];then echo $MY_GATEWAY >> ifcfg-eth0 fi if [ $Flag4 == 0 ];then echo $MY_DNS >> ifcfg-eth0 fi cp /root/ifcfg-eth0 /etc/sysconfig/network-scripts/ rm -rf /root/ifcfg-eth0
本文出自 “Jessen Liu的博文” 博客,请务必保留此出处http://zkhylt.blog.51cto.com/3638719/1716397
原文:http://zkhylt.blog.51cto.com/3638719/1716397