• 一键修改IP多实例


    一键修改IP多实例-v1

    #!/bin/bash
    #2020-03-01
    #auto change ip 
    #By Flyaway
    ######################
    grep "dhcp" /etc/sysconfig/network-scripts/ifcfg-ens33
    if [ $? -eq 0 ];then
    cat >/etc/sysconfig/network-scripts/ifcfg-ens33 <<EOF
    TYPE=Ethernet
    BOOTPROTO=static
    DEFROUTE=yes
    DEVICE=ens33
    ONBOOT=yes
    IPADDR=192.168.1.19
    PREFIX=24
    GATEWAY=192.168.1.1
    DNS1=114.114.114.114
    EOF
    fi
    cat /etc/sysconfig/network-scripts/ifcfg-ens33
    systemctl restart network
    ifconfig

    一键修改IP多实例-v2

    #!/bin/bash
    #2020-03-01
    #auto change ip 
    #By Flyaway
    ######################
    ETH_FIX="24"
    ETH_NAME="ens33"
    ETH_WAY="192.168.1.1"
    ETH_IP="192.168.1.19"
    ETH_DNS="114.114.114.114"
    ETH_BAK="/data/backup/$(date +%Y%m%d%H%M%S)"
    ETH_DIR="/etc/sysconfig/network-scripts/"
    cd $ETH_DIR
    if [ ! -d $ETH_BAK ];then
        mkdir -p $ETH_BAK
    fi
    cp ifcfg-$ETH_NAME $ETH_BAK
    grep "dhcp" ifcfg-$ETH_NAME
    if [ $? -eq 0 ];then
    cat >ifcfg-$ETH_NAME <<EOF
    TYPE=Ethernet
    BOOTPROTO=static
    DEFROUTE=yes
    DEVICE=$ETH_NAME_
    ONBOOT=yes
    IPADDR=$ETH_IP
    PREFIX=$ETH_FIX
    GATEWAY=$ETH_WAY
    DNS1=$ETH_DNS
    EOF
    fi
    cat ifcfg-$ETH_NAME
    systemctl restart network
    ifconfig $ETH_NAME

    一键修改IP多实例-v3

    #!/bin/bash
    #2020-03-01
    #auto change ip 
    #By Flyaway
    ######################
    ETH_IP="$1"
    ETH_FIX="24"
    ETH_NAME="ens33"
    ETH_WAY="192.168.1.1"
    ETH_DNS="114.114.114.114"
    ETH_DIR="/etc/sysconfig/network-scripts/"
    ETH_BAK="/data/backup/`date +%Y%m%d%H%M%S`"
    if [ $# -eq 0 ];then
        echo -e "33[32m------------------------33[0m"
        echo -e "33[32mUsage:{/bin/sh $0 192.168.1.xxx}33[0m"
        exit 1
    fi
    cd $ETH_DIR
    if [ ! -d $ETH_BAK ];then
        mkdir -p $ETH_BAK
    fi
    cp ifcfg-$ETH_NAME $ETH_BAK
    #CHECK_NUM=`grep "dhcp" ifcfg-$ETH_NAME|wc -l`
    #if [ $CHECK_NUM -eq 1 ];then
    grep "dhcp" ifcfg-$ETH_NAME
    if [ $? -eq 0 ];then
    cat >ifcfg-$ETH_NAME <<EOF
    TYPE=Ethernet
    BOOTPROTO=static
    DEFROUTE=yes
    DEVICE=$ETH_NAME_
    ONBOOT=yes
    IPADDR=$ETH_IP
    PREFIX=$ETH_FIX
    GATEWAY=$ETH_WAY
    DNS1=$ETH_DNS
    EOF
    fi
    cat ifcfg-$ETH_NAME
    systemctl restart network
    ifconfig $ETH_NAME

    一键修改IP多实例-v4

    #!/bin/bash
    #2020-03-01
    #auto change ip 
    #By Flyaway
    ######################
    ETH_IP="$1"
    ETH_FIX="24"
    ETH_NAME="ens33"
    ETH_WAY="192.168.1.1"
    ETH_DNS="114.114.114.114"
    ETH_DIR="/etc/sysconfig/network-scripts/"
    ETH_BAK="/data/backup/`date +%Y%m%d%H%M%S`"
    if [ $# -eq 0 ];then
        echo -e "33[32m------------------------33[0m"
        echo -e "33[32mUsage:{/bin/sh $0 192.168.1.xxx}33[0m"
        exit 1
    fi
    cd $ETH_DIR
    if [ ! -d $ETH_BAK ];then
        mkdir -p $ETH_BAK
    fi
    cp ifcfg-$ETH_NAME $ETH_BAK
    #CHECK_NUM=`grep "dhcp" ifcfg-$ETH_NAME|wc -l`
    #if [ $CHECK_NUM -eq 1 ];then
    grep "dhcp" ifcfg-$ETH_NAME
    if [ $? -eq 0 ];then
    cat >ifcfg-$ETH_NAME <<EOF
    TYPE=Ethernet
    BOOTPROTO=static
    DEFROUTE=yes
    DEVICE=$ETH_NAME_
    ONBOOT=yes
    IPADDR=$ETH_IP
    PREFIX=$ETH_FIX
    GATEWAY=$ETH_WAY
    DNS1=$ETH_DNS
    EOF
    else
    read -p  "The server static ipaddr alreaby exist,please ensure change other static ip?" INPUT
    if [ $INPUT == "yes" -o $INPUT == "y" -o $INPUT == "Y" ];then
    cat >ifcfg-$ETH_NAME <<EOF
    TYPE=Ethernet
    BOOTPROTO=static
    DEFROUTE=yes
    DEVICE=$ETH_NAME_
    ONBOOT=yes
    IPADDR=$ETH_IP
    PREFIX=$ETH_FIX
    GATEWAY=$ETH_WAY
    DNS1=$ETH_DNS
    EOF
    fi
    fi
    cat ifcfg-$ETH_NAME
    systemctl restart network
    ifconfig $ETH_NAME

    一键修改IP多实例-v5

    #!/bin/bash
    #2020-03-01
    #auto change ip 
    #By Flyaway
    ######################
    ETH_IP="$1"
    ETH_FIX="24"
    ETH_NAME="ens33"
    ETH_WAY="192.168.1.1"
    ETH_DNS="114.114.114.114"
    ETH_DIR="/etc/sysconfig/network-scripts/"
    ETH_BAK="/data/backup/`date +%Y%m%d%H%M%S`"
    function change_ip(){
    cat >ifcfg-$ETH_NAME <<EOF
    TYPE=Ethernet
    BOOTPROTO=static
    DEFROUTE=yes
    DEVICE=$ETH_NAME_
    ONBOOT=yes
    IPADDR=$ETH_IP
    PREFIX=$ETH_FIX
    GATEWAY=$ETH_WAY
    DNS1=$ETH_DNS
    EOF
    cat ifcfg-$ETH_NAME
    echo -e "33[32m IP 修改成功请查看:33[0m"
    }
    if [ $# -eq 0 ];then
        echo -e "33[32m------------------------33[0m"
        echo -e "33[32mUsage:{/bin/sh $0 192.168.1.xxx}33[0m"
        exit 1
    fi
    cd $ETH_DIR
    if [ ! -d $ETH_BAK ];then
        mkdir -p $ETH_BAK
    fi
    cp ifcfg-$ETH_NAME $ETH_BAK
    #CHECK_NUM=`grep "dhcp" ifcfg-$ETH_NAME|wc -l`
    #if [ $CHECK_NUM -eq 1 ];then
    grep "dhcp" ifcfg-$ETH_NAME
    if [ $? -eq 0 ];then
        change_ip
    else
        echo -e "33[32m------------------------33[0m"
        cat ifcfg-$ETH_NAME
        read -p  "The server static ipaddr alreaby exist,please ensure change other static ip?" INPUT
        if [ $INPUT == "yes" -o $INPUT == "y" -o $INPUT == "Y" ];then
            change_ip
        fi
    fi
    #cat ifcfg-$ETH_NAME
    systemctl restart network
    #ifconfig $ETH_NAME

    一键修改IP多实例-v6

    #!/bin/bash
    #2020-03-01
    #auto change ip 
    #By Flyaway
    ######################
    ETH_IP="$1"
    ETH_FIX="24"
    ETH_NAME="ens33"
    ETH_WAY="192.168.1.1"
    ETH_DNS="114.114.114.114"
    ETH_DIR="/etc/sysconfig/network-scripts/"
    ETH_BAK="/data/backup/`date +%Y%m%d%H%M%S`"
    #定义函数:
    function change_ip(){
    cat >ifcfg-$ETH_NAME <<EOF
    TYPE=Ethernet
    BOOTPROTO=static
    DEFROUTE=yes
    DEVICE=$ETH_NAME
    ONBOOT=yes
    IPADDR=$ETH_IP
    PREFIX=$ETH_FIX
    GATEWAY=$ETH_WAY
    DNS1=$ETH_DNS
    EOF
    cat ifcfg-$ETH_NAME
    echo -e "33[32m IP 修改成功请查看:33[0m"
    }
    #判断$1是否输入IP,不输入则退出:
    if [ $# -eq 0 ];then
        echo -e "33[32m------------------------33[0m"
        echo -e "33[32mUsage:{/bin/sh $0 192.168.1.xxx}33[0m"
        exit 1
    fi
    #判断目录是否存在,不存在则创建:
    cd $ETH_DIR
    if [ ! -d $ETH_BAK ];then
        mkdir -p $ETH_BAK
    fi
    #修改IP之前拷贝原网卡文件:
    cp ifcfg-$ETH_NAME $ETH_BAK
    #CHECK_NUM=`grep "dhcp" ifcfg-$ETH_NAME|wc -l`
    #if [ $CHECK_NUM -eq 1 ];then
    grep "dhcp" ifcfg-$ETH_NAME
    if [ $? -eq 0 ];then
        #引用函数:(动态IP修改为静态IP)
        change_ip
    else
        #静态IP修改为其他静态IP:
        echo -e "33[32m------------------------33[0m"
        cat ifcfg-$ETH_NAME
        read -p  "The server static ipaddr alreaby exist,please ensure change other static ip?" INPUT
        if [ $INPUT == "yes" -o $INPUT == "y" -o $INPUT == "Y" ];then
            change_ip
        fi
    fi
    #实现修改之前确认是否修改,如果不修改需要恢复原IP信息:
    read -p "The server ip change succeded,restart network.service yes or no?" INPUT
    if [ $INPUT == "yes" -o $INPUT == "y" -o $INPUT == "Y" ];then
        systemctl restart network
    else
        cp $ETH_BAK/ifcfg-$ETH_NAME $ETH_DIR
        cat ifcfg-$ETH_NAME
    fi

    一键修改IP多实例-v7

    #!/bin/bash
    #2020-03-01
    #auto change ip 
    #By Flyaway
    ######################
    ETH_IP="$1"
    ETH_FIX="24"
    ETH_NAME="ens33"
    ETH_WAY="192.168.1.1"
    ETH_DNS="114.114.114.114"
    ETH_DIR="/etc/sysconfig/network-scripts/"
    ETH_BAK="/data/backup/`date +%Y%m%d%H%M%S`"
    #定义函数:
    function change_ip(){
    cat >ifcfg-$ETH_NAME <<EOF
    TYPE=Ethernet
    BOOTPROTO=static
    DEFROUTE=yes
    DEVICE=$ETH_NAME
    ONBOOT=yes
    IPADDR=$ETH_IP
    PREFIX=$ETH_FIX
    GATEWAY=$ETH_WAY
    DNS1=$ETH_DNS
    EOF
    cat ifcfg-$ETH_NAME
    echo -e "33[32m IP 修改成功请查看:33[0m"
    }
    #判断$1是否输入IP,不输入则退出:
    if [ $# -eq 0 ];then
        echo -e "33[32m------------------------33[0m"
        echo -e "33[32mUsage:{/bin/sh $0 请输入将需要修改的IP:}33[0m"
        exit 1
    fi
    #while 判断用户输入的IP是否正确,如果不正确,让其循环输入,直到正确为止:
    while true
    do
        IP1=$(echo $ETH_IP|cut -d"." -f1)
        IP2=$(echo $ETH_IP|cut -d"." -f2)
        IP3=$(echo $ETH_IP|cut -d"." -f3)
        IP4=$(echo $ETH_IP|cut -d"." -f4)
        if [ ! -z $IP1 ];then
            if [ $IP1 -ge 1 -a $IP1 -le 255 -a $IP2 -ge 0 -a  $IP2 -le 255 -a $IP3 -ge 0 -a $IP3 -le 255 -a $IP4 -ge 0 -a $IP4 -le 255 ];then
                echo $ETH_IP
                break
            else
                echo -e "33[32m--------------------33[0m"
                read -p "Please retry input server ip: " ETH_IP
            fi
        fi
        read -p "Please retry input server ip: " ETH_IP
    done
    #判断目录是否存在,不存在则创建:
    cd $ETH_DIR
    if [ ! -d $ETH_BAK ];then
        mkdir -p $ETH_BAK
    fi
    #修改IP之前拷贝原网卡文件:
    cp ifcfg-$ETH_NAME $ETH_BAK
    #CHECK_NUM=`grep "dhcp" ifcfg-$ETH_NAME|wc -l`
    #if [ $CHECK_NUM -eq 1 ];then
    grep "dhcp" ifcfg-$ETH_NAME
    if [ $? -eq 0 ];then
        #引用函数:(动态IP修改为静态IP)
        change_ip
    else
        #静态IP修改为其他静态IP:
        echo -e "33[32m------------------------33[0m"
        cat ifcfg-$ETH_NAME
        read -p  "The server static ipaddr alreaby exist,please ensure change other static ip?" INPUT
        if [ $INPUT == "yes" -o $INPUT == "y" -o $INPUT == "Y" ];then
            change_ip
        fi
    fi
    #实现修改之前确认是否修改,如果不修改需要恢复原IP信息:
    read -p "The server ip change succeded,restart network.service yes or no?" INPUT
    if [ $INPUT == "yes" -o $INPUT == "y" -o $INPUT == "Y" ];then
        systemctl restart network
    else
        cp $ETH_BAK/ifcfg-$ETH_NAME $ETH_DIR
        cat ifcfg-$ETH_NAME
    fi
  • 相关阅读:
    微信开发者工具怎么下载
    YII框架get,post传参
    yii提示您提交的数据无法被验证
    解决[Err] 1055
    mysql日期默认值格式('0000-00-00')错误解决
    phpstorm 2018破解版 2018.2下载安装和JetbrainsCrack.jar激活教程
    PHP代码优化
    对B/S 和 C/S 架构的理解
    关于 API 安全的问题
    2020年终总结
  • 原文地址:https://www.cnblogs.com/fengyuanfei/p/13797772.html
Copyright © 2020-2023  润新知