auto_modify_ip Shell脚本安装

#!/bin/bash
#2019.7.28 18:52
#This author is DKS
#auto modify ip of linux
###############################
IP_NAME="ifcfg-ens33"
IP_DIR="/etc/sysconfig/network-scripts/"

TEMP=$(mktemp -t test.XXXXXX)
TEMP1=$(mktemp -t test1.XXXXXX)
TEMP2=$(mktemp -t test2.XXXXXX)
#
cd $IP_DIR
if [ ! -d ip_back ];then
    mkdir ip_back
fi
#获取备份编号
judge_var(){
    VAR=`find ${IP_DIR}ip_back/ -type f -name "ifcfg-ens33*"|awk -F\. '{print $2}'|sort |sed -n '$p'|grep -oE "[0-9]{0,3}"`    
    if [ $VAR -ge 1 ];then
        VAR=$(expr $VAR + 1)    
    else
        VAR=1
    fi
    IP_BACK="${IP_DIR}ip_back/ifcfg-ens33.back$VAR"
    ip_back_name="ifcfg-ens33.back$VAR"
}
#
#ip地址备份
ip_back(){
    judge_var
    cp $IP_NAME $IP_BACK 
    dialog --title "备份为" --msgbox "$ip_back_name" 10 25
}
#
#ip地址状态判断
test_ip_status(){
    STATUS=`sed -n '/BOOTPROTO/p' ${IP_NAME}|awk -F\= '{print $2}'`
    dialog --title "当前状态" --msgbox "${STATUS}" 10 20
    while [ 1 ]
    do
        dialog --inputbox "修改状态为dhcp或static" 10 20 2>$TEMP1
        if [ $? -eq 1 ];then
            BREAK=1
            break 
        fi
        status=`cat $TEMP1`
        if [[ $status == static ]];then
            local temp="static"
            BREAK=0
            break
        elif [[ $status == dhcp ]];then            
            local temp="dhcp"
            BREAK=0
            break
        else
            dialog --msgbox "输入错误" 10 20
        fi
    done
    if [ $BREAK -eq 0 ];then
        dialog --yesno "您的输入为:\n$temp " 10 20
        if [ $? -eq 0 ];then
            ip_back
            sed -i "s/BOOTPROTO=${STATUS}/BOOTPROTO=${temp}/g" $IP_NAME
            dialog --title "修改后状态" --msgbox "$(cat $IP_NAME)" 23 45
        elif [ $? -eq 1 ];then
            dialog --msgbox "已取消修改" 10 20
        fi
    fi
}
#
#ip地址修改
cat_modify_ip(){
cat >> ${IP_NAME} <<eof
IPADDR="$1"
NETMASK="255.255.255.0"
GATEWAY="192.168.2.1"
DNS1="114.114.114.114"
eof
}
modify_ip(){
    STATUS=`sed -n '/BOOTPROTO/p' ${IP_NAME}|awk -F\= '{print $2}'`
    dialog --title "当前状态" --msgbox "\n    $STATUS" 10 20
    if [[ $STATUS == static ]];then
        while [ 1 ]
        do
            dialog --title "请输入IP:" --inputbox "例:192.168.2.XXX" 10 25 2>$TEMP2
            if [ $? -eq 1 ];then
                break
            fi
            ip=$(cat $TEMP2)
            judge_ip=`echo $ip |grep -E "\<([0-9]{1,3}\.){3}([0-9]{1,3})\>"`
            if [[ $ip == $judge_ip ]];then
                local txt=0
                dialog --title "您的输入为:" --msgbox "\n$ip" 10 25 
            else
                local txt=1
            fi
            if [ $txt -eq 0 ];then
                if [ -z `sed -n "/IPADDR/p" $IP_NAME` ];then
                    ip_back
                    cat_modify_ip $ip
                    dialog --title "已修改为" --msgbox "$(cat $IP_NAME)" 25 40
                    break
                else
                    ip_back
                    old_ip=`sed -n '/IPADDR/p' $IP_NAME | awk -F\= '{print $2}'` 
                    sed -i "s/IPADDR=${old_ip}/IPADDR=${ip}/g" $IP_NAME
                    dialog --title "已修改为" --msgbox "$(cat $IP_NAME)" 25 40
                    break
                fi
            elif [ $txt -eq 1 ];then
                dialog --msgbox "输入错误" 10 20
            fi
        done
    elif [[ $STATUS == dchp ]];then
        break
    fi
}
#
#ip地址回滚
rool_back(){
    judge_var
    VAR=$(expr $VAR - 1)
    if [ $VAR le 1 ];then
        VAR=1
    fi
#获取备份
    IP_BACK="${IP_DIR}ip_back/ifcfg-ens33.back$VAR"
    ip_back_name="ifcfg-ens33.back$VAR"

    mv  $IP_BACK $IP_NAME
    VAR=$[ $VAR - 1 ]

    dialog --title "回滚备份" --msgbox "$ip_back_name" 10 25
    dialog --title "IP已回改为" --msgbox "$(cat $IP_NAME)" 25 40
}
##
#重启网卡
restart_network(){
    dialog --title "是否重启当前网络配置" --yesno "$(cat $IP_NAME)" 25 40
    if [ $? -eq 0 ];then
        service network restart &>/dev/null
        if [ $? -eq 0 ];then
            dialog --msgbox "重启成功" 10 20
        else
            dialog --msgbox "重启失败" 10 20
        fi
    elif [ $? -eq 1 ];then
        dialog --msgbox "已取消操作" 10 20
    fi
}    
##
##程序实现
while [ 1 ]
do
    dialog  --menu "自动修改服务器IP脚本" 15 30 10 1 "检测IP状态并修改" 2 "修改IP地址" 3 "IP地址回滚" 4 "重启网卡" 0 "退出" 2>$TEMP
    if [ $? -eq 1 ];then
        break
    fi
    selection=$(cat $TEMP)
    case $selection in
        1)
        test_ip_status
        ;;
        2)
        modify_ip
        ;;
        3)
        rool_back
        ;;
        4)
        restart_network
        ;;
        0)
        break
        ;;
        *)
        dialog --msgbox "输入错误" 10 20
    esac
done
rm -r $TEMP 2> /dev/null
rm -r $TEMP1 2> /dev/null
rm -r $TEMP2 2> /dev/null

 

posted @ 2019-07-29 15:02 我是氘克斯 阅读(...) 评论(...) 编辑 收藏