Hi Brad,

Thanks for zonemgr, I find it really handy and use it alot!

I noticed that there was a small issue with the netmask conversion after 
255.255.255.192, the script converts this to 27 instead of 26.  This causes an 
error message when attempting to set the network as the broadcast address would 
be greater than the global zone (when gz is 26).

Here's a test script with a fixed function called nm2cidr2.

Thanks again!
Cheers,
Graham

#!/bin/bash

##############################################################################
#
# Convert the netmask from dot notation to CIDR format
#
nm2cidr2() {
   nm=$1
   oct1=`echo $nm | cut -d. -f1`
   oct2=`echo $nm | cut -d. -f2`
   oct3=`echo $nm | cut -d. -f3`
   oct4=`echo $nm | cut -d. -f4`
     
   # Assume that most networks are class C (e.g. cidr = 24)
   cidr=24
   
     if [ $oct1 -lt 128 ]&&[ $oct2 -eq   0 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=0;
   elif [ $oct1 -lt 192 ]&&[ $oct2 -eq   0 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=1;
   elif [ $oct1 -lt 224 ]&&[ $oct2 -eq   0 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=2;
   elif [ $oct1 -lt 240 ]&&[ $oct2 -eq   0 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=3;
   elif [ $oct1 -lt 248 ]&&[ $oct2 -eq   0 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=4;
   elif [ $oct1 -lt 252 ]&&[ $oct2 -eq   0 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=5;
   elif [ $oct1 -lt 254 ]&&[ $oct2 -eq   0 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=5;
   elif [ $oct1 -lt 255 ]&&[ $oct2 -eq   0 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=7;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -lt 127 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=8;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -lt 192 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=9;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -lt 224 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=10;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -lt 240 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=11;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -lt 248 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=12;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -lt 252 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=13;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -lt 254 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=14;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -lt 255 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=15;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -lt 128 ]&&[ $oct4 -eq   
0 ];then cidr=16;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -lt 192 ]&&[ $oct4 -eq   
0 ];then cidr=17;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -lt 224 ]&&[ $oct4 -eq   
0 ];then cidr=18;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -lt 240 ]&&[ $oct4 -eq   
0 ];then cidr=19;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -lt 248 ]&&[ $oct4 -eq   
0 ];then cidr=20;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -lt 252 ]&&[ $oct4 -eq   
0 ];then cidr=21;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -lt 254 ]&&[ $oct4 -eq   
0 ];then cidr=22;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -lt 255 ]&&[ $oct4 -eq   
0 ];then cidr=23;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -eq   
0 ];then cidr=24;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -le 
128 ];then cidr=25;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -le 
192 ];then cidr=26;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -le 
224 ];then cidr=27;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -le 
240 ];then cidr=28;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -le 
248 ];then cidr=29;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -le 
252 ];then cidr=30;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -lt 
255 ];then cidr=31;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -eq 
255 ];then cidr=32;
   fi

   echo "$cidr"
}

##############################################################################
#
# Convert the netmask from dot notation to CIDR format
#
nm2cidr() {
   nm=$1
   oct1=`echo $nm | cut -d. -f1`
   oct2=`echo $nm | cut -d. -f2`
   oct3=`echo $nm | cut -d. -f3`
   oct4=`echo $nm | cut -d. -f4`
     
   # Assume that most networks are class C (e.g. cidr = 24)
   cidr=24
   
     if [ $oct1 -lt 128 ]&&[ $oct2 -eq   0 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=0;
   elif [ $oct1 -lt 192 ]&&[ $oct2 -eq   0 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=1;
   elif [ $oct1 -lt 224 ]&&[ $oct2 -eq   0 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=2;
   elif [ $oct1 -lt 240 ]&&[ $oct2 -eq   0 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=3;
   elif [ $oct1 -lt 248 ]&&[ $oct2 -eq   0 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=4;
   elif [ $oct1 -lt 252 ]&&[ $oct2 -eq   0 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=5;
   elif [ $oct1 -lt 254 ]&&[ $oct2 -eq   0 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=5;
   elif [ $oct1 -lt 255 ]&&[ $oct2 -eq   0 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=7;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -lt 127 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=8;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -lt 192 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=9;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -lt 224 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=10;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -lt 240 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=11;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -lt 248 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=12;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -lt 252 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=13;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -lt 254 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=14;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -lt 255 ]&&[ $oct3 -eq   0 ]&&[ $oct4 -eq   
0 ];then cidr=15;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -lt 128 ]&&[ $oct4 -eq   
0 ];then cidr=16;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -lt 192 ]&&[ $oct4 -eq   
0 ];then cidr=17;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -lt 224 ]&&[ $oct4 -eq   
0 ];then cidr=18;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -lt 240 ]&&[ $oct4 -eq   
0 ];then cidr=19;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -lt 248 ]&&[ $oct4 -eq   
0 ];then cidr=20;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -lt 252 ]&&[ $oct4 -eq   
0 ];then cidr=21;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -lt 254 ]&&[ $oct4 -eq   
0 ];then cidr=22;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -lt 255 ]&&[ $oct4 -eq   
0 ];then cidr=23;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -eq   
0 ];then cidr=24;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -lt 
128 ];then cidr=25;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -lt 
192 ];then cidr=26;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -lt 
224 ];then cidr=27;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -lt 
240 ];then cidr=28;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -lt 
248 ];then cidr=29;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -lt 
254 ];then cidr=30;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -lt 
255 ];then cidr=31;
   elif [ $oct1 -eq 255 ]&&[ $oct2 -eq 255 ]&&[ $oct3 -eq 255 ]&&[ $oct4 -eq 
255 ];then cidr=32;
   fi

   echo "$cidr"
}

##############################################################################
#
# Convert the netmask from CIDR format to dot notation 
#
cidr2dot() {
   cidr=$1
     if [ $cidr -ge 32 ];then netmask='255.255.255.255'
   elif [ $cidr -ge 31 ];then netmask='255.255.255.254'
   elif [ $cidr -ge 30 ];then netmask='255.255.255.252'
   elif [ $cidr -ge 29 ];then netmask='255.255.255.248'
   elif [ $cidr -ge 28 ];then netmask='255.255.255.240'
   elif [ $cidr -ge 27 ];then netmask='255.255.255.224'
   elif [ $cidr -ge 26 ];then netmask='255.255.255.192'
   elif [ $cidr -ge 25 ];then netmask='255.255.255.128'
   elif [ $cidr -ge 24 ];then netmask='255.255.255.0'
   elif [ $cidr -ge 23 ];then netmask='255.255.254.0'
   elif [ $cidr -ge 22 ];then netmask='255.255.252.0'
   elif [ $cidr -ge 21 ];then netmask='255.255.248.0'
   elif [ $cidr -ge 20 ];then netmask='255.255.240.0'
   elif [ $cidr -ge 19 ];then netmask='255.255.224.0'
   elif [ $cidr -ge 18 ];then netmask='255.255.192.0'
   elif [ $cidr -ge 17 ];then netmask='255.255.128.0'
   elif [ $cidr -ge 16 ];then netmask='255.255.0.0'
   elif [ $cidr -ge 15 ];then netmask='255.254.0.0'
   elif [ $cidr -ge 14 ];then netmask='255.252.0.0'
   elif [ $cidr -ge 13 ];then netmask='255.248.0.0'
   elif [ $cidr -ge 12 ];then netmask='255.240.0.0'
   elif [ $cidr -ge 11 ];then netmask='255.224.0.0'
   elif [ $cidr -ge 10 ];then netmask='255.192.0.0'
   elif [ $cidr -ge 9 ];then netmask='255.128.0.0'
   elif [ $cidr -ge 8 ];then netmask='255.0.0.0'
   elif [ $cidr -ge 7 ];then netmask='254.0.0.0'
   elif [ $cidr -ge 6 ];then netmask='252.0.0.0'
   elif [ $cidr -ge 5 ];then netmask='248.0.0.0'
   elif [ $cidr -ge 4 ];then netmask='240.0.0.0'
   elif [ $cidr -ge 3 ];then netmask='224.0.0.0'
   elif [ $cidr -ge 2 ];then netmask='192.0.0.0'
   elif [ $cidr -ge 1 ];then netmask='128.0.0.0'
   elif [ $cidr -ge 0 ];then netmask='0.0.0.0'
   elif [ $cidr -le 0 ];then netmask='0.0.0.0'
   fi
   echo $netmask
}

##############################################################################
#
# Convert the netmask from CIDR format to dot notation 
#
cidr2dot2() {
   cidr=$1
     if [ $cidr -ge 32 ];then netmask='255.255.255.255'
   elif [ $cidr -ge 31 ];then netmask='255.255.255.254'
   elif [ $cidr -ge 30 ];then netmask='255.255.255.252'
   elif [ $cidr -ge 29 ];then netmask='255.255.255.248'
   elif [ $cidr -ge 28 ];then netmask='255.255.255.240'
   elif [ $cidr -ge 27 ];then netmask='255.255.255.224'
   elif [ $cidr -ge 26 ];then netmask='255.255.255.192'
   elif [ $cidr -ge 25 ];then netmask='255.255.255.128'
   elif [ $cidr -ge 24 ];then netmask='255.255.255.0'
   elif [ $cidr -ge 23 ];then netmask='255.255.254.0'
   elif [ $cidr -ge 22 ];then netmask='255.255.252.0'
   elif [ $cidr -ge 21 ];then netmask='255.255.248.0'
   elif [ $cidr -ge 20 ];then netmask='255.255.240.0'
   elif [ $cidr -ge 19 ];then netmask='255.255.224.0'
   elif [ $cidr -ge 18 ];then netmask='255.255.192.0'
   elif [ $cidr -ge 17 ];then netmask='255.255.128.0'
   elif [ $cidr -ge 16 ];then netmask='255.255.0.0'
   elif [ $cidr -ge 15 ];then netmask='255.254.0.0'
   elif [ $cidr -ge 14 ];then netmask='255.252.0.0'
   elif [ $cidr -ge 13 ];then netmask='255.248.0.0'
   elif [ $cidr -ge 12 ];then netmask='255.240.0.0'
   elif [ $cidr -ge 11 ];then netmask='255.224.0.0'
   elif [ $cidr -ge 10 ];then netmask='255.192.0.0'
   elif [ $cidr -ge 9 ];then netmask='255.128.0.0'
   elif [ $cidr -ge 8 ];then netmask='255.0.0.0'
   elif [ $cidr -ge 7 ];then netmask='254.0.0.0'
   elif [ $cidr -ge 6 ];then netmask='252.0.0.0'
   elif [ $cidr -ge 5 ];then netmask='248.0.0.0'
   elif [ $cidr -ge 4 ];then netmask='240.0.0.0'
   elif [ $cidr -ge 3 ];then netmask='224.0.0.0'
   elif [ $cidr -ge 2 ];then netmask='192.0.0.0'
   elif [ $cidr -ge 1 ];then netmask='128.0.0.0'
   elif [ $cidr -ge 0 ];then netmask='0.0.0.0'
   elif [ $cidr -le 0 ];then netmask='0.0.0.0'
   fi
   echo $netmask
}


echo "--------------------------------------------------------------"

var0=0
limit=33
var1=0

while [ "$var0" -lt "$limit" ]
do
   var1=`cidr2dot $var0`
   printf "%2s: -cidr2dot->  %15s -nm2cidr->  %2s (nm2cidr2: %2s)\n" $var0 
$var1 `nm2cidr $var1` `nm2cidr2 $var1`

   let "var0 += 1"
done

echo "--------------------------------------------------------------"


exit 0
 
 
This message posted from opensolaris.org
_______________________________________________
zones-discuss mailing list
zones-discuss@opensolaris.org

Reply via email to