码迷,mamicode.com
首页 > 其他好文 > 详细

纯练手设置ip地址脚本

时间:2016-10-27 23:10:28      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:quit   设置   tin   lap   run   dhcp   ipa   egrep   pat   

#!/bin/bash
IFO() {
        read -p "Configure $line Network card ( ‘yes‘or‘no‘ )?" CDN </dev/tty
        if [ "${CDN}" == yes ];then
                ADD_D
        else
                echo "quiting"
                exit
        fi
}
NET_W() {
while :;do
STR=`cat /etc/udev/rules.d/70-persistent-net.rules | grep SUBSYSTEM= | cut -d‘ ‘ -f7 | cut -d= -f2 | sed ‘s/"//g‘`
echo "$STR" | while read line;do
	echo $line	
done
	echo "please input configure network card?"
	read name
	line=$name
	IFO
	continue
done
}
IPC() {
echo $1 | grep -oP ‘((\d)+.){3}\d+‘ &>/dev/null
until [ $? -eq 0 ];do
	echo -e "\033[33mIP address error! Please run again...\033[0m"
	exit 4
	
done
}

CAT_C() {
	BOOT_D=`cat /etc/sysconfig/network-scripts/ifcfg-$line | grep BOOTPROTO | awk -F ‘=‘ ‘{print $2}‘`
if [ $BOOT_D == dhcp ];then
        echo -e "now is \033[31m$BOOT_D\033[0m Type"
elif [ $BOOT_D == none ];then
        echo -e "now is \033[31m$BOOT_D\033[0m Type"
elif [ $BOOT_D == static ];then
        echo -e "now is \033[31m$BOOT_D\033[0m Type"
else
        echo "option error..."
fi
}
ADD_D() {

[ -f /etc/sysconfig/network-scripts/ifcfg-$line ] && echo -e "configure network card:\033[1;34m $line\033[0m"
if [ $? -eq 1 ];then
	echo -e "#######################\nNot found $line configure file\n###################################"
	continue
fi
CAT_D=`cat /etc/sysconfig/network-scripts/ifcfg-$line | grep -v "#" | egrep "IPADDR|NETMASK|GATEWAY" || echo "Parameter is null"`
echo -e "\033[31m*******************************\033[0m\033[1;34m\nA total of\n$STR\n\nnow ncurrent netwrok $line:\n$CAT_D\033[0m"
CAT_C
echo -e "\033[31m*************************************\033[0m"
read -p "Please input IPADDR :" II </dev/tty
IPC $II 
read -p "Please input GATEWAY :" GA </dev/tty
IPC $GA
read -p "Please input NETMASK :" NE </dev/tty
IPC $NE
        III="IPADDR=$II"
        GAA="GATEWAY=$GA"
        NEE="NETMASK=$NE"
	PATH1="/etc/sysconfig/network-scripts/ifcfg-$line"
	IPADDR_D=`cat /etc/sysconfig/network-scripts/ifcfg-$line | grep "IPADDR="`
	NETMASK_D=`cat /etc/sysconfig/network-scripts/ifcfg-$line | grep "NETMASK="`
	GATEWAY_D=`cat /etc/sysconfig/network-scripts/ifcfg-$line | grep "GATEWAY="`
	sed -i "s/$IPADDR_D/$III/g" $PATH1 &>/dev/null || echo $III >> $PATH1
	sed -i "s/$NETMASK_D/$NEE/g" $PATH1 &>/dev/null || echo $NEE >> $PATH1 
	sed -i "s/$GATEWAY_D/$GAA/g" $PATH1 &>/dev/null || echo $GAA >> $PATH1
        sed -i ‘s/ONBOOT=no/ONBOOT=yes/g‘ /etc/sysconfig/network-scripts/ifcfg-$line
        sed -i ‘s/NM_CONTROLLED=yes/NM_CONTROLLED=no/g‘ /etc/sysconfig/network-scripts/ifcfg-$line
	sed -i ‘s/BOOTPROTO=dhcp/BOOTPROTO=static/g‘ /etc/sysconfig/network-scripts/ifcfg-$line
	sed -i ‘s/BOOTPROTO=none/BOOTPROTO=static/g‘ /etc/sysconfig/network-scripts/ifcfg-$line
        ifdown $line && ifup $line &>/dev/null && echo "restart network card finished"
        ASA=`cat /etc/sysconfig/network-scripts/ifcfg-$line`
	echo -e "\033[35m$ASA\033[0m"
}

NET_W 

纯练手设置ip地址脚本

标签:quit   设置   tin   lap   run   dhcp   ipa   egrep   pat   

原文地址:http://www.cnblogs.com/ligao/p/6005836.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!