纯练手设置ip地址脚本

#!/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 
posted @ 2016-10-27 22:12  quicksand.F  阅读(280)  评论(0编辑  收藏  举报