LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-07-2011, 01:47 AM   #1
myegorov86
LQ Newbie
 
Registered: Sep 2011
Posts: 6

Rep: Reputation: Disabled
Network ID calculator in a bash script


Hello,
I need to create a script that receives 2 variables (ip address, subnet mask) and calculates a network id

can anyone help??

Thanks!
 
Old 09-07-2011, 03:23 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
What have you tried so far? What is your definition of a 'network id'?
 
Old 09-07-2011, 03:26 AM   #3
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
delete (replied to wrong post)
 
Old 09-07-2011, 03:34 AM   #4
indeliblestamp
Member
 
Registered: Feb 2006
Distribution: Fedora
Posts: 341
Blog Entries: 3

Rep: Reputation: 40
There are existing tools that can do this, e.g. sipcalc. You could try using that, unless your intention is to write the script yourself.
 
Old 09-07-2011, 03:36 AM   #5
myegorov86
LQ Newbie
 
Registered: Sep 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
This is an example of one

http://www.techzoom.net/tools/networ...-calculator.en

You can see there a field named "Network ID", so this is basically what i need; receive 2 variables and based on them show the output of the so called "Network ID"

Thanks!
 
Old 09-07-2011, 03:57 AM   #6
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Example: 192.168.10.10 with 255.255.255.0

The basic logic (not bash code) you'll need is...

(split IP into 4 components)
A=192
B=168
C=10
D=10

(split netmask into 4 components)
W=255
X=255
Y=255
Z=0

(calculate the result from each individual component where & is bitwise AND)
R1=$A & $W
R2=$B & $X
R3=$C & $Y
R4=$D & $Z

(Then compile and return the result)
echo "The network ID is $R1.$R2.$R3.$R4"

I'm just giving you that since this looks like homework.
 
Old 09-07-2011, 04:15 AM   #7
myegorov86
LQ Newbie
 
Registered: Sep 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Its not a homework

I'm writing a script that will help certain users to change some configuration in solaris without editing files.
The file i'm talking about is /etc/netmasks, which have the format of "network_id netmask".
I want the user to insert only the desired IP address and the subnet mask.
I'm not that strong in scripting, i can understand the logic of it but i don't know how to execute it.
So please, if you can just tell me how to it, i'll be very happy.

p.s
I wish it was a homework... unfortunately i need to write it as soon as possible, so i don't have much time to investigate it

Thanks!
 
Old 09-07-2011, 08:00 AM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Some tips that might help:

Code:
Use filename expansion:
${ipaddress}="192.168.1.100"

This extracts the 1st octet and assigns it to the first element of the octet[] array:
${octet[0]]=${ipaddress%%.*}

This strips of the 1st octet.  Repeated 3 times, you can extract each element using the code just above:
$ippart=${ipaddress#*.}

Either in a loop, or sequential lines of code, you can extract the first octet, and assign it to an element; strip off the first octet, and repeat.
${ippart}=${ipaddress}
${octet[0]]=${ippart%%.*}
$ippart=${ippart#*.}
${octet[1]]=${ippart%%.*}
...


You can use the '&' operator to calculate a bitwise AND.  You can use this to mask the IP address with the netmask, returning the network address part:
netpart[0]=$((${ipnum[0] & ${netmasknum[0]}))

To perform integer operations, enclose expressions in $(( .. )).  Otherwise, a value might be a string instead.

Last edited by jschiwal; 09-07-2011 at 08:10 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash CIDR calculator genderbender Programming 18 10-29-2014 10:13 AM
[SOLVED] How to invoke the calculator "bc" in bash script for floating point arithmetic? bluesmodular Programming 2 05-15-2010 11:58 PM
Shell Script Calculator gazagelen Linux - Newbie 7 05-05-2010 06:45 AM
Permission bit calculator script ... ??? newbie01.linux Linux - General 3 05-03-2010 04:01 AM
programming a calculator with bash xiekke Programming 4 03-18-2006 11:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration