iptools.ksh (ksh)

#!/usr/bin/ksh
#iptools.ksh
# -----------------------------------------------------------------------------------------
# definitions
# -----------------------------------------------------------------------------------------
 
networks="2620 10.0.0.0 8\n2630 192.168.0.0 16\n2640 169.254.0.0 16"
 
ip_address1="10.0.168.33"
ip_address2="192.168.168.34"
ip_address3="169.254.168.35"
 
# -----------------------------------------------------------------------------------------
# functions
# -----------------------------------------------------------------------------------------
 
# convert dotted IP Address to binary
# in:   dotted IP Address (192.168.3.25)
# out:  ip_binary
ip2b ()
{
        ip_address=$1
        o1=`echo $ip_address | cut -d "." -f1`
        o2=`echo $ip_address | cut -d "." -f2`
        o3=`echo $ip_address | cut -d "." -f3`
        o4=`echo $ip_address | cut -d "." -f4`
        typeset -Z8 b1=`echo "obase=2; ibase=10;  $o1" | bc`
        typeset -Z8 b2=`echo "obase=2; ibase=10;  $o2" | bc`
        typeset -Z8 b3=`echo "obase=2; ibase=10;  $o3" | bc`
        typeset -Z8 b4=`echo "obase=2; ibase=10;  $o4" | bc`
        #ip_binary=$b1$b2$b3$b4
        echo "$b1$b2$b3$b4"
}
 
# convert dotted IP Address to decimal
# in:   dotted IP Address (192.168.3.25)
# out:  ip_decimal
ip2d ()
{
        ip_address=$1
        o1=`echo $ip_address | cut -d "." -f1`
        o2=`echo $ip_address | cut -d "." -f2`
        o3=`echo $ip_address | cut -d "." -f3`
        o4=`echo $ip_address | cut -d "." -f4`
        echo "obase=10; ibase=10;  $o1*256*256*256 + $o2*256*256 + $o3*256 +$o4" | bc
}
 
# convert binary IP Address to dotted
# in:  binary IP address 
# out: ip_address 
b2ip ()
{
        ip_binary=$1
        o1=`echo $ip_binary | cut -c1-8`
        o2=`echo $ip_binary | cut -c9-16`
        o3=`echo $ip_binary | cut -c17-24`
        o4=`echo $ip_binary | cut -c25-32`
        d1=`echo "obase=10; ibase=2; $o1" | bc` 
        d2=`echo "obase=10; ibase=2; $o2" | bc` 
        d3=`echo "obase=10; ibase=2; $o3" | bc` 
        d4=`echo "obase=10; ibase=2; $o4" | bc` 
        echo "$d1.$d2.$d3.$d4"
}
 
# calculates the broadcast address of a given network 
# in:   dotted IP Address (192.168.3.25), network bits (25)
# out:  ip_decimal
getBroadcastAddress ()
{
        network_ip=$1
        bits=$2
 
        ip_binary=$( ip2b $network_ip )
        broadcast=`echo $ip_binary | cut -c1-$bits`
        i=$bits
        while [ $i -lt 32 ]
        do
                broadcast=$broadcast"1"
                i=$i+1
        done
 
        echo "$broadcast"
}
 
# searches all networks for the matching IP range and returns the VLAN ID
# in:   IP address of host (192.168.25.3)
# out:  vlan_id, vlan_desc
getVlanId ()
{
        ip_decimal_host=$( ip2d $1 )
 
        echo $networks |
        while read n
        do
                vlan=`echo $n | cut  -d " " -f1`
                net=`echo $n | cut -d " " -f2`
                bits=`echo $n | cut -d " " -f3`
 
                ip_decimal_first=$( ip2d $net )
                broadcast_ip_bin=$( getBroadcastAddress $net $bits )
                ip_decimal_last=$( ip2d $( b2ip  $broadcast_ip_bin ) )
 
                if [ $ip_decimal_first -le $ip_decimal_host -a $ip_decimal_host -le $ip_decimal_last ]
                then
                        echo "$vlan"
                        return 0
                fi
        done
}
 
# ========================================================================================
# main
# ========================================================================================
 
#do stuff
#-----------------------------------------------------------------------------------------
echo "Demo format conversion of IP Address"
echo "------------------------------------"
echo "Orig: $ip_address1\t\t\t$ip_address2\t\t\t\t$ip_address3"
echo "Bin : $( ip2b $ip_address1 )\t$( ip2b $ip_address2 )\t$( ip2b $ip_address3 )"
echo "Dec : $( ip2d $ip_address1 )\t\t\t\t$( ip2d $ip_address2 )\t\t\t\t$( ip2d $ip_address3 )"
echo "\n"
echo "Demo IP Addres network lookup"
echo "-------------------------------"
echo "$ip_address1 belongs to VLAN $( getVlanId $ip_address1 )"
echo "$ip_address2 belongs to VLAN $( getVlanId $ip_address2 )"
echo "$ip_address3 belongs to VLAN $( getVlanId $ip_address3 )"