#!/bin/bash # when provided an interface, this will determine the subnet and the bits for tha tsubnet if [[ "X" == "X$1" ]] then echo "Usage: ${0} " exit 1 fi eth="$1" ipaddr=$(ifconfig ${eth} | grep 'inet addr' | awk -F':' '{print $2}' | awk '{print $1}' ) mask=$(ifconfig ${eth} | grep 'inet addr' | awk -F':' '{print $4}') # echo "${ipaddr} ${mask}" line=$(echo ${ipaddr} | sed 's/\./ /g') set ${line} p1="$1" p2="$2" p3="$3" p4="$4" # (( ipnum = p4 + 256 * (p3 + 256 * (p2 + 256 * p1)) )) line=$(echo ${mask} | sed 's/\./ /g') set ${line} p1="$1" p2="$2" p3="$3" p4="$4" # (( masknum = p4 + 256 * (p3 + 256 * (p2 + 256 * p1)) )) # echo $ipnum $masknum line=$(echo ${ipaddr} | sed 's/\./ /g') set ${line} ip1="$1" ip2="$2" ip3="$3" ip4="$4" line=$(echo ${mask} | sed 's/\./ /g') set ${line} m1="$1" m2="$2" m3="$3" m4="$4" # echo "mask: $m1 $m2 $m3 $m4" ((n1 = ip1 & m1)) ((n2 = ip2 & m2)) ((n3 = ip3 & m3)) ((n4 = ip4 & m4)) network="$n1.$n2.$n3.$n4" # echo "network: $n1 $n2 $n3 $n4" ((x1 = 255 - m1)) ((x2 = 255 - m2)) ((x3 = 255 - m3)) ((x4 = 255 - m4)) (( tot = x4 + 256 * (x3 + 256 * (x2 + 256 * x1)) )) ((bits=0)) while ((0 < tot)) do ((bits = bits + 1)) ((tot >>= 1)) done ((msk = 32 - bits)) # echo "subnet: $x1 $x2 $x3 $x4 /$msk" echo "${network}/${msk}"