Application of script seven: Subnet computing

Source: Internet
Author: User
Tags ibase

function: calculates the subnet IP range and the number of available IPs

implementation: use BC tools to calculate, please pre-install, do not check

Insufficient: the IP address and mask format are not judged

use: provide two parameters, address and mask, output format: Host number-network number [number of available IP]


Demonstrate:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7F/00/wKioL1cPf1fjYzF4AACNhyJYDA8531.png "title=" 1.png " alt= "Wkiol1cpf1fjyzf4aacnhyjyda8531.png"/>


Code area:

#/bin/bash#  ly#  ------------------#  copyright 2016.04.14 lingyi   ([email protected])  QQ: 1519952564#  ------------------------------------------ ------------------#   "subnet masks  ' calculation" #将ip地址转换为二进制, 32 characters containing only 0 and 1 get_2s () {Local  i j s stayfor  ((i=1; i<=4; i++));  do stay=$ ( echo  " obase=2; ibase=10;  ' echo $1 | cut -d '. '  -f$i ' " | bc ) #地址的每个段转换为二进制应为八位数, the number of digits on the left side plus" 0 "complement [[ ${#stay} -ne 8 ]  && { for  ((j=1; j<=8-l; j++));  do stay= "0${stay}";  Done; }s=${s} $staydoneecho   $s} #将一个32个只包含0和1的字符转换为十进制, and the format of the IP address get_10s () {local n t# Truncates the string by one field per eight bits and converts to decimal for n in 0 8 16 24; do t=${t}.$ ( echo  "Obase=10; ibase=2;${1:n:8}" |bc ) doneecho  $T  | sed  ' s/^.//'} #将地址和掩码转换为二进制字符串ip_2s =$ ( get_2s $1 ) mask_2s=$ ( get_2s $2 ) # Compare the binary of the address and mask, get the binary string for the host number for ((i=0; i<32; i++));  doa=${ip_2s:i:1}b=${mask_2s:i:1} #模拟二进制的与操作 [[ ${b} -eq 1 ]] && comp_2s=${comp_2s} $a  | |  comp_2s=${comp_2s}0done# calculate the number of "0" in the mask N, the number of available IPs is  2 N minus 2 (host number and network number is not available) sum_1=$ ( echo ${mask_2s } | grep -o  ' 1 '  | wc -l ) useful_ip_sum=$ (( $ ( echo  2^$ ((32-sum_1)) " | bc "  -2 ) #网络号即将主机号的主机位全置为1broadcast_2s =${ip_2s:0:sum_1}$ (for  ( i=0; i<32-sum_1; i++); do echo -n  ' 1 ';  done) #将二进制转换为十进制 and output echo in address format  -e  "\033[1;34m$ (get_10s  $comp _2s)  - $ (get_10s  $broadcast _2s)  [ $ Useful_ip_sum ]\033[0m "








This article is from the "retrograde person" blog, please be sure to keep this source http://lingyi.blog.51cto.com/2837715/1763902

Application of script seven: Subnet computing

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.