Translate

Archives

Calculate IPv4 Subnet Mask and Prefix

Here is a simple ksh93 script which I recently wrote to calculate the subnet mask and prefix when given a range of IPv4 addresses. It makes use of the bit manipulation facilities available in ksh93. #!/bin/ksh93 [[ $# != 2 ]] && { echo "Usage: $0 ipaddress1 ipaddress2" exit 1 } SaveIFS=$IFS IFS="./" typeset -a IParr1=($1) typeset -a IParr2=($2) IFS=$SaveIFS typeset -i2 ip11=${IParr1[0]} typeset -i2 ip12=${IParr1[1]} typeset -i2 ip13=${IParr1[2]} typeset -i2 ip14=${IParr1[3]} typeset -i2 ip21=${IParr2[0]} typeset -i2 ip22=${IParr2[1]} typeset -i2 ip23=${IParr2[2]} typeset -i2 ip24=${IParr2[3]} mask=0; match=1 for ((arr=1; arr < 5; arr++)) do binary_value1="" binary_value2="" for ((b=7; b >= 0;