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>>16&NBSP;&&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>>24&NBSP;&&NBSP;0X000000FF)) ((IPHEX[2]=BCASTADDR>>16&NBSP;&&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