Visit Our UNIX and Linux User Community

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
# 1  
Old 02-20-2009
how to calculate netwrk from IP address and netmask using Bitwise AND in shell script


I am having two variables

To get network number, I know that a bitwise & will help.
networkno=IP & netmask

My code is
networkno="$ip" & "$netmask"
echo $networkno

I am getting the following error
./[5]: not found.

What's the mistake in the code.
# 2  
Old 02-20-2009
The ampersand (&) puts processes in the background, so you are putting networkno=$ip in the background, then trying to execute

What is it that you want to accomplish? What's the output you are expecting?
If you give and, are you expecting the output to be Or .1? Or .0 ? or ?
# 3  
Old 02-20-2009
Actually you have 2 mistakes
  1. To do bitwise operations, you have to do it within the delimiters for arithmetic evaluation $(( ... ))
  2. You'll first have to convert your IP address to it's integer representation ( would become 177605633 [0x0A960C01]), since you can't do arithmetic on strings.
You probably could do it in the shell, but I guess you'll be quicker writing a small C program that does that for you.
# 4  
Old 02-20-2009
Hi System Shock & pludi,

thanks for the inputs.
What I want is
if IP="" and netmask="",
I want networkno to be 10.150.12.

I do not know how to change strings into integers.
Also, I have to use shell scripting only.

Please help me with the code to accomplish the above requirement
# 5  
Old 02-20-2009
Originally Posted by chaitanyapn
[...] I have to use shell scripting only.[...]
Why Shell code only? A C program to calculate IP-Address related stuff (or about anything on a bit manipulation level) can easily be written in ANSI C which will compile on any platform, and it's quite trivial to do so (was one of my first C assignments in school)

As for the conversion, it would be something like this pseudo-code.
Split on '.'
For each element
    Result *= 256
    Result += element

So would be converted to 177605633. Do this for both the IP and the Netmask and do your &-magic. To get something back do:
Result=177605632 # IP & NM
While Result > 256
    Byte = Result % 256
    Result /= 256

which will give you the least significant byte first, so you'll probably have to switch them.
# 6  
Old 02-20-2009
Here is how to do it using ksh93.

typeset -i2 mask=255

[[ $# != 2 ]] && {
   echo "Usage: $0 ipaddress subnetmask"
   exit 1

typeset -a IParr=($1)
typeset -a NMarr=($2)

typeset -i2 ipbin1=${IParr[0]}
typeset -i2 ipbin2=${IParr[1]}
typeset -i2 ipbin3=${IParr[2]}
typeset -i2 ipbin4=${IParr[3]}

typeset -i2 nmbin1=${NMarr[0]}
typeset -i2 nmbin2=${NMarr[1]}
typeset -i2 nmbin3=${NMarr[2]}
typeset -i2 nmbin4=${NMarr[3]}

echo "       IP Address: $1"
echo "      Subnet Mask: $2"
echo "  Network Address: $((ipbin1 & nmbin1)).$((ipbin2 & nmbin2)).$((ipbin3 & nmbin3)).$((ipbin4 & nmbin4))"
echo "Broadcast Address: $((ipbin1 | (mask ^ nmbin1))).$((ipbin2 | (mask ^ nmbin2))).$((ipbin3 | (mask ^ nmbin3))).$((ipbin4 | (mask ^ nmbin4)))"

exit 0

Here is the output for the IP address and subnet mask you supplied followed by the output for a sightly different subnet mask (just to show the difference)
$ ./calculate-address

       IP Address:
      Subnet Mask:
  Network Address:
Broadcast Address:

$ ./calculate-address

       IP Address:
      Subnet Mask:
  Network Address:
Broadcast Address:

This User Gave Thanks to fpmurphy For This Post:
# 7  
Old 02-22-2009
Hi fpmurhy,
The code worked fine ..Smilie

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: Result will be: 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 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 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 (2 Replies)
Discussion started by: rhacodactylus
2 Replies

Featured Tech Videos