Juil 042013
 

Works on Bash only


cidr2dec() {
cidr=$1
totalmask= ; bits=

for ((i=$cidr; $i>0; i--)); do bits="${bits}1" ; done
for ((i=$((32 - $cidr)); $i>0; i--)); do bits="${bits}0" ; done

for i in 1 9 17 25; do
bitmask=$(echo $bits |cut -c${i}-$(($i + 7)))
totalmask=${totalmask}$(echo 'obase=10; ibase=2;' $bitmask |bc).
done

echo ${totalmask%%.}
}

 


$ for i in 16 24 26 27 32 ; do cidr2dec $i ; done
255.255.0.0
255.255.255.0
255.255.255.192
255.255.255.224
255.255.255.255