how to calculate netwrk from IP address and netmask using Bitwise AND in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to calculate netwrk from IP address and netmask using Bitwise AND in shell script
# 8  
Old 05-07-2009
scripts for IP / CIDR / Subnet calculations

fpmurphy, thanks for your code. I've now changed it to work with bash and included a few other IP/CIDR/Subnet mask related functions.

Sorry if my code is not so optimized, but this was my first try Smilie
I'm trying to put together code to run on a WRT54G router with DD-WRT firmware. Eventually I want it to remove redundant/encapsulated routing table entries.
Code:
#!/bin/bash

# Convert IP to int
function ip_to_int() {
    SaveIFS=$IFS
    IFS=.
    typeset -a IParr=($1)
    IFS=$SaveIFS

    result=0
    for ((i=0;i<4;i+=1)); do
        result=$(($result * 256))
        result=$((${IParr[$i]} + $result))
    done

    echo $result
}

# Convert IP from int
function int_to_ip() {
    result=$1
    byte=""
    for ((i=0;i<3;i+=1)); do
        byte=.$(($result % 256))$byte
        result=$(($result / 256))
    done
    echo $result$byte
}

# Calculate number of addresses in subnet 
function cidr_to_netsize() {
    echo $((1 << 32 - $1))
}

# Calculate bitmask for the 'host' part
function cidr_to_hostmask() {
    echo $(($(($((1 << $1)) - 1)) << $((32 - $1))))
}

# Get the Network destination from the IP & Subnet mask
function get_network_address() {
    SaveIFS=$IFS
    IFS=.
    typeset -a IParr=($1)
    typeset -a NMarr=($2)
    IFS=$SaveIFS

    echo $((${IParr[0]} & ${NMarr[0]})).$((${IParr[1]} & ${NMarr[1]})).$((${IParr[2]} & ${NMarr[2]})).$((${IParr[3]} & ${NMarr[3]}))
}

# Get the broadcast address from the IP & Subnet mask
function get_broadcast_address() {
    SaveIFS=$IFS
    IFS=.
    typeset -a IParr=($1)
    typeset -a NMarr=($2)
    IFS=$SaveIFS

    echo $((${IParr[0]} | (255 ^ ${NMarr[0]}))).$((${IParr[1]} | (255 ^ ${NMarr[1]}))).$((${IParr[2]} | (255 ^ ${NMarr[2]}))).$((${IParr[3]} | (255 ^ ${NMarr[3]})))
}

[[ $# != 2 ]] && {
   echo "Usage: $0 CIDR IP-Address"
   exit 1
}

# Given values
cidr=$1
ip=$2

# Calculations
netsize=$(cidr_to_netsize $cidr)
hostmask=$(cidr_to_hostmask $cidr)
subnet=$(int_to_ip $hostmask)
int_ip=$(ip_to_int $2)
ip_from_int=$(int_to_ip $int_ip)
netaddr=$(get_network_address $ip $subnet)
broadcast=$(get_broadcast_address $ip $subnet)

# Output
echo "Given CIDR: $cidr"
echo "    CIDR to NETWORK SIZE: $netsize"
echo "    CIDR to HOST MASK: $hostmask"
echo "    HOST MASK to SUBNET MASK: $subnet"
echo 
echo "Given IP: $ip"
echo "    IP to INT: $int_ip"
echo "    INT to IP: $ip_from_int"
echo
echo "Given IP & SUBNET MASK: $ip & $subnet"
echo "    IP & SUBNET MASK to NETWORK ADDRESS: $netaddr"
echo "    IP & SUBNET MASK to BROADCAST ADDRESS: $broadcast"
exit 0

Sample output:
Code:
$ ./ip2int 16 192.168.10.15
Given CIDR: 16
        CIDR to NETWORK SIZE: 65536
        CIDR to HOST MASK: 4294901760
        HOST MASK to SUBNET MASK: 255.255.0.0

Given IP: 192.168.10.15
        IP to INT: 3232238095
        INT to IP: 192.168.10.15

Given IP & SUBNET MASK: 192.168.10.15 & 255.255.0.0
        IP & SUBNET MASK to NETWORK ADDRESS: 192.168.0.0
        IP & SUBNET MASK to BROADCAST ADDRESS: 192.168.255.255

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash cript to calculate summarize address

Hi, I need to write a bash script that when i enter two ip address, it will calculate summerize address for them. Examlpe: 192.168.1.27/25 192.168.1.129/25 Result will be: 192.168.1.0/24 can you help me with this script? I even dont know how to start with it (3 Replies)
Discussion started by: Miron
3 Replies

2. UNIX for Dummies Questions & Answers

Script shell calculate mean arrival request duration

hello, I have implemented this command : tshark -eth0 -T fiels -e frame.time et sip.Request-Line -z sip,stat > test2.txt the result of this command : test.txt: Aug 27, 2013 23:06:47.334270000 INVITE Aug 27, 2013 23:06:47.335045000 SIP/2.0 401 Unauthorized Aug 27, 2013... (1 Reply)
Discussion started by: Amouna
1 Replies

3. Shell Programming and Scripting

Shell script to calculate the max cpu usage from the main script

Hi All, I have a script which does report the cpu usuage, there are few output parameter/fields displayed from the script. My problem is I have monitor the output and decide which cpu number (column 2) has maximum value (column 6). Since the output is displayed/updated every seconds, it's very... (1 Reply)
Discussion started by: Optimus81
1 Replies

4. Shell Programming and Scripting

Shell script to calculate difference between 2 dates

shell script to calculate difference between 2 dates (3 Replies)
Discussion started by: gredpurushottam
3 Replies

5. Shell Programming and Scripting

Script shell, how to calculate percentage?

hello, please can you help me. jj and kk are two numbers which are the result of an sql program. I would like to calculate the ratio jj/kk*100. I have done this: ratio=$((jj/kk * 100)) or ratio=`expr $jj \/ expr $kk) but the result is 0 What can i do? Thanks for help. (3 Replies)
Discussion started by: likeaix
3 Replies

6. Shell Programming and Scripting

Shell script to calculate the size of files

Dear all, Please help me to write a script that can calculate the size of files. For example: I have a directory which contain thousands of files. I need to know the size of files that their name begin with abc_123 Thank all!! (4 Replies)
Discussion started by: hainguyen1402
4 Replies

7. Shell Programming and Scripting

Calculate Date from shell script

I have a txt file which now has the following output Sat Mar 6 04:00:01 Sat Mar 6 04:02:09 processing 585 Sat Mar 6 17:00:01 Sat Mar 6 17:00:58 processing 109 Sun Mar 7 04:00:01 Sun Mar 7 04:00:51 processing 309 Sun Mar 7 17:00:01 Sun Mar 7 17:00:41 processing 48 I want output as... (1 Reply)
Discussion started by: gubbu
1 Replies

8. Solaris

ifconfig - making netmask & broadcast address permanent?

hi, I am trying to configure one of my interfaces, but after reboot - i lose the changes to the netmask & broadcast address. I have added an entry in /etc/netmasks, but it doesnt pick up the new settings. any ideas - much appreciated. before reboot: eri0:... (3 Replies)
Discussion started by: badoshi
3 Replies

9. Shell Programming and Scripting

Shell Script calculate the hits ?!

could you please find a solution for this a complex command using pipes to Calculate the number of hits per client in Squid log file (access.log), the command should display most active hosts first. Line example 1197979501.787 1 10.1.14.62 TCP_NEGATIVE_HIT/404 1463 GET http://url The... (0 Replies)
Discussion started by: anything
0 Replies

10. Shell Programming and Scripting

script for netmask check

I want develop a script which should also check validity of netmask. e.g. /etc/netmasks 10.15.20.30 255.255.255.224 How can I check which IP adresses are valid for this netmask? I think the best is use logical operations. 224 is 1 1 1 0 0 0 0 0 so is valid from 10.15.20.31... (2 Replies)
Discussion started by: rhacodactylus
2 Replies
Login or Register to Ask a Question