Benutzer-Werkzeuge

Webseiten-Werkzeuge


programming:shell:iptools:iptbash

iptools.sh (bash)

#!/usr/bin/bash
# iptools.sh
# ----------------------------------------------------------------------------------------
# definitions
# ----------------------------------------------------------------------------------------
 
declare -a networks
#            VLAN NET BITS"
networks[0]="2620 10.0.0.0 8"
networks[1]="2630 192.168.0.0 16"
networks[2]="2640 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 (10.0.168.33)
# out:  binary IP Address (11000000101010001010100000100010)
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`
 
        b1=`echo "obase=2; ibase=10;  $o1" | bc`
        b2=`echo "obase=2; ibase=10;  $o2" | bc`
        b3=`echo "obase=2; ibase=10;  $o3" | bc`
        b4=`echo "obase=2; ibase=10;  $o4" | bc`
 
        echo "`printf "%08s" "$b1"``printf "%08s" "$b2"``printf "%08s" "$b3"``printf "%08s" "$b4"`"
 
}
 
# convert dotted IP Address to decimal
# in:   dotted IP Address (10.0.168.33)
# out:  decimal IP Address (167815201)
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 (11000000101010001010100000100010) 
# out: dotted IP Address (10.0.168.33) 
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 network IP Address (10.0.0.0), network bits (8)
# out:  decimal broadcast IP Address (167815201)
net2bc()
{
        network_ip=$1
        bits=$2
 
        ip_binary=$( ip2b $network_ip )
        broadcast=`echo $ip_binary | cut -c1-$bits`
        for ((i=$bits;i<32;i++)); do
                broadcast=$broadcast"1"
        done
 
        echo "$broadcast"
}
 
# searches all networks in the list ($networks) for the matching IP range 
# and returns the appropriate VLAN ID
# in:   IP address of host (10.0.168.33)
# out:  VLAN ID (2620)
getVlanId ()
{
        ip_decimal_host=$( ip2d $1 )
 
        inetworks=${#networks[@]}
 
        for ((i=0;i<$inetworks;i++)); do
 
                vlan=`echo ${networks[i]} | cut  -d " " -f1`
                net=`echo ${networks[i]} | cut -d " " -f2`
                bits=`echo ${networks[i]} | cut -d " " -f3`
 
                ip_decimal_first=$( ip2d $net )
                broadcast_ip_bin=$( net2bc $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
        #echo ${networks[${i}]}
        done
}
 
# ==========================================================================================
# main
# ==========================================================================================
 
#do stuff
#-------------------------------------------------------------------------------------
echo "Demo format conversion of IP Address"
echo "------------------------------------"
printf "Orig: %32s %32s %32s\n" "$ip_address1" "$ip_address2" "$ip_address3"
printf "Bin:  %32s %32s %32s\n" "$( ip2b $ip_address1 )" "$( ip2b $ip_address2 )" "$( ip2b $ip_address3 )"
printf "Dec:  %32s %32s %32s\n" "$( ip2d $ip_address1 )" "$( ip2d $ip_address2 )" "$( 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 )"
programming/shell/iptools/iptbash.txt · Zuletzt geändert: von 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki