zoukankan      html  css  js  c++  java
  • 一键修改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
  • 相关阅读:
    04 16 团队竞技(第二场) 赛后总结
    HDU 1863 畅通工程 克鲁斯卡尔算法
    HUD 2544 最短路 迪杰斯特拉算法
    hdoj 4526 威威猫系列故事——拼车记
    HDU 3336 Count the string 查找匹配字符串
    Linux command line exercises for NGS data processing
    肿瘤基因组学数据库终结者:cBioPortal---转载
    lncRNA研究利器之"TANRIC"
    Python的collections模块中的OrderedDict有序字典
    python set
  • 原文地址:https://www.cnblogs.com/fengyuanfei/p/13797772.html
Copyright © 2011-2022 走看看