Shell怎么自动修改CentOS6网卡配置文件
更新:HHH   时间:2023-1-7


本篇内容介绍了“Shell怎么自动修改CentOS6网卡配置文件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

#!/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

“Shell怎么自动修改CentOS6网卡配置文件”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注天达云网站,小编将为大家输出更多高质量的实用文章!

返回开发技术教程...