Calculate all your interfaces’ netmasks

This script for Solaris prints these values,
1- the interface name
2- MAC Address
3- IPMP Group (if any)
4- IP
5- netmask in hex
6- netmask in decimal
7- netmask length
bash-3.00$ ./netmask.ksh
lo0,,,127.0.0.1, ff000000,255.0.0.0,8
ce0,0:3:ba:84:dd:a5,management,198.148.129.96,ffffffc0,255.255.255.192,26
ce0:1,,,198.148.129.110,ffffffc0,255.255.255.192,26
ce0:2,,,198.148.129.98,ffffffc0,255.255.255.192,26
ce3,0:3:ba:84:dd:a8,,192.168.5.11,ffffff00,255.255.255.0,24
ce3:1,,,192.168.130.254,fffff000,255.255.240.0,20
qfe0,0:3:ba:4:45:4,management,198.148.129.97,ffffffc0,255.255.255.192,26
qfe3,0:3:ba:4:45:7,,192.168.132.254,fffff000,255.255.240.0,20
This is the code for netmask.ksh, notice that we also have an unused funcion, Len2Mask, which is very
useful to calculate a netmask in decimal when you’re given the netmask lenght, like “24”, or “16”:
#!/bin/ksh
IFCONFIG=/usr/sbin/ifconfig
ECHO=/usr/ucb/echo
integer G=0
raiseP(){
x=$1
y=$2
integer total=1
integer j=0
while ((j < y));
do
(( total*=x ))
(( j = j + 1 ))
done
$ECHO $total
return $total
}
L2M(){
integer nmask=$1
if [ nmask -lt 1 ];then
$ECHO -n “0”
return “0”
fi
110/433
integer ncalc=0
integer x=7
while [ $x -ge 0 ];do
integer P=`raiseP 2 $x`
(( ncalc= ncalc + P ));
(( nmask=nmask – 1 ));
G=$nmask
if [ $nmask -lt 1 ];then
$ECHO -n $ncalc
return $ncalc
fi
(( x= x – 1 ));
done
$ECHO -n $ncalc
return $ncalc
}
Len2Mask(){
L=$1
if [ $L -lt 0 -o $L -gt 32 ];then
$ECHO “Your mask length can only be 0 – 32”
break
fi
L2M $L
$ECHO -n “.”
L2M $G
$ECHO -n “.”
L2M $G
$ECHO -n “.”
L2M $G
}
Mask2Len(){
M=$1
m[0]=`$ECHO $M | awk -F”.” ‘{ print $1 }’`
m[1]=`$ECHO $M | awk -F”.” ‘{ print $2 }’`
m[2]=`$ECHO $M | awk -F”.” ‘{ print $3 }’`
m[3]=`$ECHO $M | awk -F”.” ‘{ print $4 }’`
loop=0
mask=0
while [ $loop -lt 4 ];do
div=256
while [ $div -gt 1 ];do
let “div=$div / 2”
let “test=${m[$loop]} – div”
if [ ! $test -lt 0 ];then
let “mask=$mask +1”
m[$loop]=$test
else
break
fi
done
let “loop=$loop+1”
done
$ECHO “$mask”
111/433
}
$IFCONFIG -a| grep “:” |egrep -v “ether” | awk ‘{ print $1 }’ | sed ‘s/(.*):/1/’ | while read i; do
F=/tmp/${i}.$$
$IFCONFIG $i > $F
I=`cat $F | egrep -v “:|group” | awk ‘{ print $2 }’`
E=`cat $F | egrep “ether” | awk ‘{ print $2 }’`
GP=`cat $F | egrep “groupname” | awk ‘{ print $2 }’`
NMH=`cat $F | grep -v “:” | awk ‘{ print $4 }’`
rm $F
TMP=`$ECHO $NMH | sed ‘s/../ 0x&/g’`
NMD=`printf “%d.%d.%d.%dn” $TMP`
M2L=`Mask2Len $NMD`
$ECHO “$i,$E,$GP,$I,$NMH,$NMD,$M2L”
done
112/433

Leave a Reply

Your email address will not be published. Required fields are marked *