Centos6 multi-segment Ip add script and centos6 multi-segment ip script

Source: Internet
Author: User

Centos6 multi-segment Ip add script and centos6 multi-segment ip script

#! /Bin/bash

Export device = 'ifconfig | grep eth0 | head-n 1 | awk '{print ($1 )}''
Export ipcfg_pre = "/etc/sysconfig/network-scripts/ifcfg-eth1-range"
Awktmp = 'mktemp'
Msg = 'mktemp'
Ip_txt = 'mktemp'
Ifcfgs = "$ (find $ {ip;_pre %/*}-name $ {ip;_pre ##*/}*)"
If test! -Z "$ ifcfgs"; then
Echo "found that the following existing configuration files are deleted (yes/no )? "
Echo "$ ifcfgs"
Select var in "yes" "no"
Do
If test "$ var" = "yes"; then
Rm-f $ ifcfgs
Break;
Else
Break;
Fi

Done
Fi

# Save the script to a temporary file and no longer use the. awk file.

Cat> $ awktmp <EOF
#! /Usr/bin/awk-f
BEGIN {
Ip1__pre = "$ {ip1__pre }"
# You need to modify ip1__pre as the path prefix.
Device = ENVIRON ["device"]
Clone_num = 1;
Numofpc [24] = 253
Numofpc [24] = 125
Numofpc [26] = 61
Numofpc [27] = 29
Numofpc [28] = 13
Numofpc [29] = 5
}
Function ip_str (iplist ){
Return sprintf ("% s. % s", ip_list [1], ip_list [2], ip_list [3], ip_list [4]);
}
Function gen_mask (num, mask_bin, _ ARGVEND _, sum, bits ){
# Convert a mask from a number to an array
_ I = 1;
Mask_bin [1] = 0;
For (; _ I * 8 <= num & _ I <= 4; _ I ++ ){
Mask_bin [_ I] = 255;
}
Bits = num-(_ i-1) * 8;
Sum = 0;
For (; bits> 0; bits --){
Sum = sum + 2 ^ (8-bits );
}
Mask_bin [_ I] = sum;
_ I ++;
For (; _ I <= 4; _ I ++ ){
Mask_bin [_ I] = 0;
}
}
{
Ipfile = sprintf ("% s", ip1__pre, NR );
Split (\ $0, ip ,"/");
Ip_mask = ip [2];
Split (ip [1], ip_list ,".");
# The ip_list list stores the four ip byte values
Ip_start = ip_list [4] + 2;
Ip_end = ip_start + numofpc [ip_mask]-1;
Printf ("DEVICE = % s \ n", device)> ipfile
Printf ("BOOTPROTO = static \ n")> ipfile
Printf ("CLONENUM_START = % d \ n", clone_num)> ipfile
Ip_list [4] = ip_start;
Printf ("IPADDR_START = % s \ n", ip_str (ip_list)> ipfile
Ip_list [4] = ip_end;
Printf ("IPADDR_END = % s \ n", ip_str (ip_list)> ipfile
Clone_num + = numofpc [ip_mask];
Printf ("ONBOOT = yes \ n")> ipfile
Gen_mask (ip_mask, ip_list)
Netmask = ip_str (ip_list)
Printf ("NETMASK = % s \ n", netmask)> ipfile
}

EOF
Chmod a + x $ awktmp

Msg = 'mktemp'
Cat> $ msg <EOF
Enter the IP address and mask for one row per line (blank lines are not allowed)
For example:
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
# Service network restart

Test-e $ ip_txt & awk-f $ awktmp $ ip_txt
Service network restart
Rm-f $ awktmp $ msg $ ip_txt
Rm-f ipcfg. sh
========================================================== ==================

This script only applies

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

Add IP segments in this format. Check whether the script Nic name is consistent with the machine Nic name.

Use method, named ipcfg. sh
1. You will be prompted whether the IP file already exists, so you can choose whether to delete it. We only use it for the first time to add. Try not to add more.
2. Press enter, then press I to enter the editing mode, and enter the IP segment. A single line of iP segments. Do not have spaces. Save.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.