首页 > 系统服务 > 详细

Shell自动修改CentOS6网卡配置文件

时间:2015-11-24 18:45:57      阅读:287      评论:0      收藏:0      [点我收藏+]
#!/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

Shell自动修改CentOS6网卡配置文件

原文:http://zkhylt.blog.51cto.com/3638719/1716397

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!