IP network address and broadcast address calculation for shell scripts

Source: Internet
Author: User

This is an old-fashioned exercise, shell script to practice a lot, want to practice shift calculation, there is the following script.


#!/bin/bashippattern= "(\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \>\.) {3}\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \> "maskpattern=" [1-9]| [12] [0-9]|3[01] "while :d o  echo -e -n " Please input a legal ip  address [such as 192.168.1.1/16]:  "  read userInput     ipmask= ' echo  $userInput  | egrep  "$IPPattern/$maskPattern" '     # such as 192.168.1.1/24  if [ -z  "$IPMask"  ] ;then     echo  "PLEASE&NBSP;INPUT&NBSP;THE&NBSP;RIGHT&NBSP;FORMAT.&NBSP;[SUCH&NBSP;AS&NBSP;192.168.1.1/ 1-31]  "    continue  fi    ipaddr= ' echo  $IPMask  | cut -d/ -f1 '   iptype[1]= ' echo  $IPAddr  | cut -d. -f1 '   iptype[2]= ' echo  $IPAddr  | cut -d.&nBsp;-f2 '   iptype[3]= ' echo  $IPAddr  | cut -d. -f3 '   iptype[4]= ' echo   $IPAddr  | cut -d. -f4 '   mask= ' echo  $IPMask  | cut -d/  -f2 '   echo  ' ip address is ${iptype[1]}*${iptype[2]}*${iptype[3]}*${iptype[ 4]} , mask is  $mask  . "      ((iphex[1]=iptype[1]<<24))    ((iphex[2]=iptype[2]<<16))     ((iphex[3]=iptype[3]<<8))    ((iphex[4]=iptype[4))      #192  c0  ; 1 1 1   ((Iph=${iphex[1]}+${iphex[2]}+${iphex[3]}+${iphex[4]})   # echo  $iph    #0xffffffff    #declare  -i strmask1=4294967295  declare  -i strMask1=0xffffffff   #echo   $strMask 1   ((strmask1=strmask1<< (32- Mask)    #echo   $strMask 1   (strmask2=~STRMASK1)    #echo   $strMask 2   ((networkaddr=iph & strmask1))     ((bcastaddr=  (IPH&NBSP;|&NBSP;STRMASK2)  &  0xffffffff)    #echo  $ networkaddr | awk  ' {printf  "%x\n", $ '    #echo   $bcastAddr  | awk   ' {printf  '%x\n ', $ '        ((iphex[1]=networkaddr>>24 & &NBSP;0X000000FF)    ((IPHEX[2]=NETWORKADDR&GT;&GT;16&NBSP;&AMP;&NBSP;0X000000FF))    ((Iphex [3]=networkaddr>>8 & 0x000000ff)    (iphex[4]=networkaddr &  0X000000FF))   echo -e  "Network address   : ${iphex[1]}.${iphex[2" }.${IPHEX[3]}.${IPHEX[4]} "     ((IPHEX[1]=BCASTADDR&GT;&GT;24&NBSP;&AMP;&NBSP;0X000000FF))    ((IPHEX[2]=BCASTADDR&GT;&GT;16&NBSP;&AMP;&NBSP;0X000000FF))    ((iphex[3]=bcastaddr> >8 & 0x000000FF)    ((iphex[4]=bcastaddr & 0x000000ff))   echo -e  "broadcast &NBSP;ADDRESS&NBSP;:&NBSP;${IPHEX[1]}.${IPHEX[2]}.${IPHEX[3]}.${IPHEX[4]} "done



This article from "The End of Nanshan" blog, declined to reprint!

IP network address and broadcast address calculation for shell scripts

Related Article

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.