zoukankan      html  css  js  c++  java
  • Ubuntu 添加整段多IP脚本


    #!/bin/bash
    export device
    echo "请输入网络设备名:"
    devices="`ifconfig -s|sed '1d'|awk '{print($1)}'`"
    select device in $devices;
    do
    if [ -n "$device" ];then
    break
    fi
    done
    export ipcfg_pre="/etc/network/interfaces"
    export ipcfg_bak=${ipcfg_pre}.bak-by-autoipcfg
    awktmp=`mktemp`
    msg=`mktemp`
    ip_txt=`mktemp`

    if test ! -e "$ipcfg_bak";then
    cp "$ipcfg_pre" "$ipcfg_bak" || exit $?
    echo "未发现配置文件备份,自动备份文件名为:"
    echo "$ipcfg_bak"
    fi
    cat "$ipcfg_bak">"$ipcfg_pre"
    #重置 配置文件为默认配置
    cat >$awktmp <<EOF
    #!/usr/bin/awk -f
    BEGIN{
    ipcfg_pre="${ipcfg_pre}"
    device=ENVIRON["device"]
    number_of_dev=1;
    numofpc[24]=253
    numofpc[26]=61
    numofpc[27]=29
    numofpc[28]=13
    numofpc[29]=5

    netmask_table[0]="0.0.0.0"
    netmask_table[1]="128.0.0.0"
    netmask_table[2]="192.0.0.0"
    netmask_table[3]="224.0.0.0"
    netmask_table[4]="240.0.0.0"
    netmask_table[5]="248.0.0.0"
    netmask_table[6]="252.0.0.0"
    netmask_table[7]="254.0.0.0"
    netmask_table[8]="255.0.0.0"
    netmask_table[9]="255.128.0.0"
    netmask_table[10]="255.192.0.0"
    netmask_table[11]="255.224.0.0"
    netmask_table[12]="255.240.0.0"
    netmask_table[13]="255.248.0.0"
    netmask_table[14]="255.252.0.0"
    netmask_table[15]="255.254.0.0"
    netmask_table[16]="255.255.0.0"
    netmask_table[17]="255.255.128.0"
    netmask_table[18]="255.255.192.0"
    netmask_table[19]="255.255.224.0"
    netmask_table[20]="255.255.240.0"
    netmask_table[21]="255.255.248.0"
    netmask_table[22]="255.255.252.0"
    netmask_table[23]="255.255.254.0"
    netmask_table[24]="255.255.255.0"
    netmask_table[25]="255.255.255.128"
    netmask_table[26]="255.255.255.192"
    netmask_table[27]="255.255.255.224"
    netmask_table[28]="255.255.255.240"
    netmask_table[29]="255.255.255.248"
    netmask_table[30]="255.255.255.252"
    netmask_table[31]="255.255.255.254"

    }
    function ip_str(iplist){
    return sprintf("%s.%s.%s.%s",ip_list[1],ip_list[2],ip_list[3],ip_list[4]);
    }
    {
    ipfile=ipcfg_pre
    split($0,ip,"/");
    ip_mask=ip[2];
    split(ip[1],ip_list,".");
    split(ip[1],masklist,".");
    #ip_list 列表保存了 ip 的四个 字节值
    ip_start=ip_list[4]+2;
    ip_end=ip_start+numofpc[ip_mask]-1;
    ip_list[4]++
    gateway=ip_str(ip_list)
    netmask=netmask_table[ip_mask]

    for(i=ip_start;i<=ip_end;i++){
    device_name=sprintf("%s:%s",device,number_of_dev )
    printf(" ")>>ipfile
    printf("auto %s ",device_name)>>ipfile
    printf("iface %s inet static ",device_name)>>ipfile
    ip_list[4]=i
    printf("address %s ",ip_str(ip_list))>>ipfile
    printf("netmask %s ",netmask)>>ipfile
    printf("gateway %s ",gateway)>>ipfile
    number_of_dev++
    }

    }
    EOF

    chmod a+x $awktmp
    msg=`mktemp`

    cat >$msg <<EOF
    请输入IP地址和掩码每行一个(不可出现空白行)
    如:
    45.34.89.40/29
    23.91.3.0/28
    23.91.8.32/28
    23.91.18.160/28
    23.91.28.128/28
    EOF
    vi +":!cat $msg" $ip_txt

    test -e $ip_txt && awk -f $awktmp $ip_txt
    sudo /etc/init.d/networking restart
    rm -f $msg $awktmp $ip_txt
    rm -f ubuntu.sh

  • 相关阅读:
    微服务划分的姿势
    微服务的时间和成本去哪儿了
    假如你是架构师,你要做些什么
    说透代码评审
    从哲学层面浅谈计算机学习方法论
    分布式事务:两阶段提交与三阶段提交
    敏捷史话(四):敏捷是人的天性 —— Arie van Bennekum
    敏捷史话(三):笃定前行的勇者——Ken Schwaber
    敏捷史话(二):Scrum社区的悲剧性损失——Mike Beedle
    敏捷史话(一):用一半的时间做两倍的事——Scrum之父Jeff Sutherland
  • 原文地址:https://www.cnblogs.com/uyahs/p/7670400.html
Copyright © 2011-2022 走看看