{"id":497,"date":"2011-01-28T00:30:38","date_gmt":"2011-12-04T13:51:50","guid":{"rendered":"http:\/\/kraftek.com\/blog\/calculate-all-your-interfaces-netmasks\/"},"modified":"2011-01-28T00:30:38","modified_gmt":"2011-12-04T13:51:50","slug":"calculate-all-your-interfaces-netmasks","status":"publish","type":"post","link":"\/blog\/index.php\/calculate-all-your-interfaces-netmasks\/","title":{"rendered":"Calculate all your interfaces&#8217; netmasks"},"content":{"rendered":"<p>This script for Solaris prints these values,<br \/>\n1- the interface name<br \/>\n2- MAC Address<br \/>\n3- IPMP Group (if any)<br \/>\n4- IP<br \/>\n5- netmask in hex<br \/>\n6- netmask in decimal<br \/>\n7- netmask length<br \/>\nbash-3.00$ .\/netmask.ksh<br \/>\nlo0,,,127.0.0.1, ff000000,255.0.0.0,8<br \/>\nce0,0:3:ba:84:dd:a5,management,198.148.129.96,ffffffc0,255.255.255.192,26<br \/>\nce0:1,,,198.148.129.110,ffffffc0,255.255.255.192,26<br \/>\nce0:2,,,198.148.129.98,ffffffc0,255.255.255.192,26<br \/>\nce3,0:3:ba:84:dd:a8,,192.168.5.11,ffffff00,255.255.255.0,24<br \/>\nce3:1,,,192.168.130.254,fffff000,255.255.240.0,20<br \/>\nqfe0,0:3:ba:4:45:4,management,198.148.129.97,ffffffc0,255.255.255.192,26<br \/>\nqfe3,0:3:ba:4:45:7,,192.168.132.254,fffff000,255.255.240.0,20<br \/>\nThis is the code for netmask.ksh, notice that we also have an unused funcion, Len2Mask, which is very<br \/>\nuseful to calculate a netmask in decimal when you&#8217;re given the netmask lenght, like &#8220;24&#8221;, or &#8220;16&#8221;:<br \/>\n#!\/bin\/ksh<br \/>\nIFCONFIG=\/usr\/sbin\/ifconfig<br \/>\nECHO=\/usr\/ucb\/echo<br \/>\ninteger G=0<br \/>\nraiseP(){<br \/>\nx=$1<br \/>\ny=$2<br \/>\ninteger total=1<br \/>\ninteger j=0<br \/>\nwhile ((j &lt; y));<br \/>\ndo<br \/>\n(( total*=x ))<br \/>\n(( j = j + 1 ))<br \/>\ndone<br \/>\n$ECHO $total<br \/>\nreturn $total<br \/>\n}<br \/>\nL2M(){<br \/>\ninteger nmask=$1<br \/>\nif [ nmask -lt 1 ];then<br \/>\n$ECHO -n &#8220;0&#8221;<br \/>\nreturn &#8220;0&#8221;<br \/>\nfi<br \/>\n110\/433<br \/>\ninteger ncalc=0<br \/>\ninteger x=7<br \/>\nwhile [ $x -ge 0 ];do<br \/>\ninteger P=`raiseP 2 $x`<br \/>\n(( ncalc= ncalc + P ));<br \/>\n(( nmask=nmask &#8211; 1 ));<br \/>\nG=$nmask<br \/>\nif [ $nmask -lt 1 ];then<br \/>\n$ECHO -n $ncalc<br \/>\nreturn $ncalc<br \/>\nfi<br \/>\n(( x= x &#8211; 1 ));<br \/>\ndone<br \/>\n$ECHO -n $ncalc<br \/>\nreturn $ncalc<br \/>\n}<br \/>\nLen2Mask(){<br \/>\nL=$1<br \/>\nif [ $L -lt 0 -o $L -gt 32 ];then<br \/>\n$ECHO &#8220;Your mask length can only be 0 &#8211; 32&#8221;<br \/>\nbreak<br \/>\nfi<br \/>\nL2M $L<br \/>\n$ECHO -n &#8220;.&#8221;<br \/>\nL2M $G<br \/>\n$ECHO -n &#8220;.&#8221;<br \/>\nL2M $G<br \/>\n$ECHO -n &#8220;.&#8221;<br \/>\nL2M $G<br \/>\n}<br \/>\nMask2Len(){<br \/>\nM=$1<br \/>\nm[0]=`$ECHO $M | awk -F&#8221;.&#8221; &#8216;{ print $1 }&#8217;`<br \/>\nm[1]=`$ECHO $M | awk -F&#8221;.&#8221; &#8216;{ print $2 }&#8217;`<br \/>\nm[2]=`$ECHO $M | awk -F&#8221;.&#8221; &#8216;{ print $3 }&#8217;`<br \/>\nm[3]=`$ECHO $M | awk -F&#8221;.&#8221; &#8216;{ print $4 }&#8217;`<br \/>\nloop=0<br \/>\nmask=0<br \/>\nwhile [ $loop -lt 4 ];do<br \/>\ndiv=256<br \/>\nwhile [ $div -gt 1 ];do<br \/>\nlet &#8220;div=$div \/ 2&#8221;<br \/>\nlet &#8220;test=${m[$loop]} &#8211; div&#8221;<br \/>\nif [ ! $test -lt 0 ];then<br \/>\nlet &#8220;mask=$mask +1&#8221;<br \/>\nm[$loop]=$test<br \/>\nelse<br \/>\nbreak<br \/>\nfi<br \/>\ndone<br \/>\nlet &#8220;loop=$loop+1&#8221;<br \/>\ndone<br \/>\n$ECHO &#8220;$mask&#8221;<br \/>\n111\/433<br \/>\n}<br \/>\n$IFCONFIG -a| grep &#8220;:&#8221; |egrep -v &#8220;ether&#8221; | awk &#8216;{ print $1 }&#8217; | sed &#8216;s\/(.*):\/1\/&#8217; | while read i; do<br \/>\nF=\/tmp\/${i}.$$<br \/>\n$IFCONFIG $i &gt; $F<br \/>\nI=`cat $F | egrep -v &#8220;:|group&#8221; | awk &#8216;{ print $2 }&#8217;`<br \/>\nE=`cat $F | egrep &#8220;ether&#8221; | awk &#8216;{ print $2 }&#8217;`<br \/>\nGP=`cat $F | egrep &#8220;groupname&#8221; | awk &#8216;{ print $2 }&#8217;`<br \/>\nNMH=`cat $F | grep -v &#8220;:&#8221; | awk &#8216;{ print $4 }&#8217;`<br \/>\nrm $F<br \/>\nTMP=`$ECHO $NMH | sed &#8216;s\/..\/ 0x&amp;\/g&#8217;`<br \/>\nNMD=`printf &#8220;%d.%d.%d.%dn&#8221; $TMP`<br \/>\nM2L=`Mask2Len $NMD`<br \/>\n$ECHO &#8220;$i,$E,$GP,$I,$NMH,$NMD,$M2L&#8221;<br \/>\ndone<br \/>\n112\/433<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-497","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts\/497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=497"}],"version-history":[{"count":0,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts\/497\/revisions"}],"wp:attachment":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=497"},{"taxonomy":"post_tag","embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}