LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 02-14-2011, 08:01 AM   #1
Teibidh
LQ Newbie
 
Registered: Feb 2011
Posts: 7

Rep: Reputation: 0
Bash IF using IP subnets?


Hello, I'd like to say thanks in advance for any assistance with this. I'm somewhere between Novice and I have no idea what I'm doing with bash scripts. I'm writing a script to deploy images using partimage on my company's desktops, and while I have just about everything else figured out I have one issue left.

Each of our 4 sites that will be using this disc will have a deployment server due to the fact that our sites have dedicated point to point links that our business traffic is conducted on. I need to be able to determine what site I'm at based on IP subnet and set a variable based on this determination. What I don't know is how to get the IP address in to an if statement, and properly determine subnet. For example:

192.168.1.0/16 - 192.168.7.0/16 need to use DEPLSERV01
192.168.8.0/16 - 192.168.16.0/16 use DEPLSERV02
192.168.17.0/16 - 192.168.24.0/16 use DEPLSERV03
... etc ...

Can anyone help me get this done? Thanks!
 
Old 02-14-2011, 08:10 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
SUBNET=$(ifconfig eth0 | awk -F. '{if (/inet addr/) { print $3 }}')
if [[ $SUBNET > 16 ]]
then
SERVER=DEPLSERV03
elif [[ $SUBNET > 7 ]]
SERVER=DEPLSERV02
else
SERVER=DEPLSERV01
fi

but your networks don't really make sense, I assume you mean /24 instead of /16? as ALL of those networks are in 192.168.0.0/16
 
  


Reply



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
subnets and routing evilted Linux - Networking 19 08-20-2009 05:16 AM
subnets how to master Linux - Networking 10 12-10-2007 01:51 PM
subnets and subnetting johnMG Linux - Networking 25 12-20-2005 10:05 PM
help on subnets HappyGilmore Linux - Networking 12 07-07-2005 09:33 AM
newbie and subnets nkeever Linux - Newbie 10 05-03-2005 06:29 PM

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

All times are GMT -5. The time now is 12:33 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