Solved

req: script to insert router in this file

Posted on 2011-02-14
19
451 Views
Last Modified: 2012-05-11
Hello,

I have a netsh dump from a Microsoft DHCP server

In this file I have various networks that dont define a router ( option 3). There set at 0.0.0.0

like below:

# NET ---
# NET 128.163.112.0/24
# WARN - option missing a value (router-discovery)
# WARN - option missing a value (mask-supplier)
Dhcp Server 1.1.1.1 add scope 128.163.112.0 255.255.255.0 "128.163.112.0-24-pot-ob11-rp" ""
Dhcp Server 1.1.1.1 Scope 128.163.112.0 set optionvalue 6 STRING "128.163.3.10" "128.163.1.6"
Dhcp Server 1.1.1.1 Scope 128.163.112.0 set optionvalue 3 STRING "0.0.0.0"
Dhcp Server 1.1.1.1 Scope 128.163.112.0 set optionvalue 51 STRING "345600"
Dhcp Server 1.1.1.1 Scope 128.163.112.0 set optionvalue 44 STRING "128.163.2.77" "128.163.1.77"

What I would like to request is a perl script/Or shell of some sort that would read this file and where you see Option 3 string "0.0.0.0"

to insert the router IP based on the # NET 128.163.112.0/24
so in this case the router would be 128.163.112.1

So the final section would look like

# NET ---
# NET 128.163.112.0/24
# WARN - option missing a value (router-discovery)
# WARN - option missing a value (mask-supplier)
Dhcp Server 1.1.1.1 add scope 128.163.112.0 255.255.255.0 "128.163.112.0-24-pot-ob11-rp" ""
Dhcp Server 1.1.1.1 Scope 128.163.112.0 set optionvalue 6 STRING "128.163.3.10" "128.163.1.6"
Dhcp Server 1.1.1.1 Scope 128.163.112.0 set optionvalue 3 STRING "128.163.112.1"
Dhcp Server 1.1.1.1 Scope 128.163.112.0 set optionvalue 51 STRING "345600"
Dhcp Server 1.1.1.1 Scope 128.163.112.0 set optionvalue 44 STRING "128.163.2.77" "128.163.1.77"

Attached is the entire file.

Thank you very much for your help.

dhcp.txt
0
Comment
Question by:richsark
  • 10
  • 9
19 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 34893909
Please try this:


while ( <> )
{
	if ( /\# NET / ) { $net = $'; $net =~ s/[\r\n]//g; }
	else
	{
		s/set optionvalue 3 STRING \"0.0.0.0\"/set optionvalue 3 STRING \"$net\"/;
	}
	print $_;
}

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 34893930
Hi,

So would I create a file.pl and my file

so I would run as

add-router.pl dhcp.txt > router-add.conf
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34893935
Yes.  Sorry to leave it ambiguous.  Create a perl script file from the posted code (add-router.pl) and execute just as you said.  You may need to say "perl" depending on your setup.  This should always work:

perl add-router.pl dhcp.txt > router-add.conf
0
 
LVL 1

Author Comment

by:richsark
ID: 34893948
Hi,

ok, almost there, Its not quite right :)

# WARN - option missing a value (router-discovery)
# WARN - option missing a value (mask-supplier)
Dhcp Server 1.1.1.1 add scope 128.163.10.0 255.255.255.0 "128.163.10.0-24-me-206-rs" ""
Dhcp Server 1.1.1.1 Scope 128.163.10.0 set optionvalue 6 STRING "128.163.3.10" "128.163.1.6"
Dhcp Server 1.1.1.1 Scope 128.163.10.0 set optionvalue 3 STRING "128.163.10.0/24"
Dhcp Server 1.1.1.1 Scope 128.163.10.0 set optionvalue 51 STRING "345600"
Dhcp Server 1.1.1.1 Scope 128.163.10.0 set optionvalue 44 STRING "128.163.2.77" "128.163.1.77"

Should be:

# WARN - option missing a value (router-discovery)
# WARN - option missing a value (mask-supplier)
Dhcp Server 1.1.1.1 add scope 128.163.10.0 255.255.255.0 "128.163.10.0-24-me-206-rs" ""
Dhcp Server 1.1.1.1 Scope 128.163.10.0 set optionvalue 6 STRING "128.163.3.10" "128.163.1.6"
Dhcp Server 1.1.1.1 Scope 128.163.10.0 set optionvalue 3 STRING "128.163.10.1"
Dhcp Server 1.1.1.1 Scope 128.163.10.0 set optionvalue 51 STRING "345600"
Dhcp Server 1.1.1.1 Scope 128.163.10.0 set optionvalue 44 STRING "128.163.2.77" "128.163.1.77"
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34893975
while ( <> )
{
	if ( /\# NET ([0-9\.]+)/ ) { $net = $1; }
	else
	{
		s/set optionvalue 3 STRING \"0.0.0.0\"/set optionvalue 3 STRING \"$net\"/;
	}
	print $_;
} 

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 34893981
Hi,

Still missing the .1 on that line

# WARN - option missing a value (router-discovery)
# WARN - option missing a value (mask-supplier)
Dhcp Server 1.1.1.1 add scope 128.163.1.0 255.255.255.128 "128.163.1.0-25-mcv-059-rp" ""
Dhcp Server 1.1.1.1 Scope 128.163.1.0 set optionvalue 6 STRING "128.163.3.10" "128.163.1.6"
Dhcp Server 1.1.1.1 Scope 128.163.1.0 set optionvalue 3 STRING "128.163.1.0"
Dhcp Server 1.1.1.1 Scope 128.163.1.0 set optionvalue 51 STRING "345600"
Dhcp Server 1.1.1.1 Scope 128.163.1.0 set optionvalue 44 STRING "128.163.2.77" "128.163.1.77"

Should be in this case

# WARN - option missing a value (router-discovery)
# WARN - option missing a value (mask-supplier)
Dhcp Server 1.1.1.1 add scope 128.163.1.0 255.255.255.128 "128.163.1.0-25-mcv-059-rp" ""
Dhcp Server 1.1.1.1 Scope 128.163.1.0 set optionvalue 6 STRING "128.163.3.10" "128.163.1.6"
Dhcp Server 1.1.1.1 Scope 128.163.1.0 set optionvalue 3 STRING "128.163.1.1"
Dhcp Server 1.1.1.1 Scope 128.163.1.0 set optionvalue 51 STRING "345600"
Dhcp Server 1.1.1.1 Scope 128.163.1.0 set optionvalue 44 STRING "128.163.2.77" "128.163.1.77"
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34894010
Starting to get a little sloppy (assumes it is always /24, for example), but this may get you what you need:

while ( <> )
{
	if ( /\# NET ([0-9]+\.[0-9]+\.[0-9]+)/ ) { $net = $1; }
	else
	{
		s/set optionvalue 3 STRING \"0.0.0.0\"/set optionvalue 3 STRING \"$net\.1\"/;
	}
	print $_;
} 

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 34894083
Cool, awesome,

So in a case were it maybe other then /24, it will compensate?
0
 
LVL 1

Author Comment

by:richsark
ID: 34894105
Hi Buddy,

I came across a /27 and /26 and it did not work for that? Can you see please :)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:sjklein42
ID: 34894115
The posted code simply takes the first three (out of four) dotted #NET numbers and then appends .1 to the end.

What should it do if the bitmask length is not 24?

I guess I don't really understand where the ".1" comes from.  Is this  the gateway address?

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34894126
Please give an example of the desired output for the /27 and /26 cases.  I also see /22, /23 and /25.  How about those?

0
 
LVL 1

Author Comment

by:richsark
ID: 34894145
Sure,

Original for /26

# NET ---
# NET 172.24.134.0/26
# WARN - option missing a value (router-discovery)
# WARN - option missing a value (mask-supplier)
Dhcp Server 1.1.1.1 add scope 172.24.134.0 255.255.255.192 "172.24.134.0-26-mcv-059-rp" ""
Dhcp Server 1.1.1.1 Scope 172.24.134.0 set optionvalue 6 STRING "128.163.3.10" "128.163.1.6"
Dhcp Server 1.1.1.1 Scope 172.24.134.0 set optionvalue 3 STRING "0.0.0.0"
Dhcp Server 1.1.1.1 Scope 172.24.134.0 set optionvalue 51 STRING "345600"
Dhcp Server 1.1.1.1 Scope 172.24.134.0 set optionvalue 44 STRING "128.163.2.77" "128.163.1.77"

Original For /27

# NET ---
# NET 172.24.164.64/27
# WARN - option missing a value (router-discovery)
# WARN - option missing a value (mask-supplier)
Dhcp Server 1.1.1.1 add scope 172.24.164.64 255.255.255.224 "172.24.164.64-27-mcv-059-rp" ""
Dhcp Server 1.1.1.1 Scope 172.24.164.64 set optionvalue 6 STRING "128.163.3.10" "128.163.1.6"
Dhcp Server 1.1.1.1 Scope 172.24.164.64 set optionvalue 3 STRING "0.0.0.0"
Dhcp Server 1.1.1.1 Scope 172.24.164.64 set optionvalue 51 STRING "345600"
Dhcp Server 1.1.1.1 Scope 172.24.164.64 set optionvalue 44 STRING "128.163.2.77" "128.163.1.77"

For /27, your script did this

# NET ---
# NET 128.163.139.80/27
# WARN - option missing a value (router-discovery)
# WARN - option missing a value (mask-supplier)
Dhcp Server 1.1.1.1 add scope 128.163.139.80 255.255.255.224 "128.163.139.80-27-mcv-059-rs" ""
Dhcp Server 1.1.1.1 Scope 128.163.139.80 set optionvalue 6 STRING "128.163.3.10" "128.163.1.6"
Dhcp Server 1.1.1.1 Scope 128.163.139.80 set optionvalue 3 STRING "128.163.139.1"
Dhcp Server 1.1.1.1 Scope 128.163.139.80 set optionvalue 51 STRING "345600"
Dhcp Server 1.1.1.1 Scope 128.163.139.80 set optionvalue 44 STRING "128.163.2.77" "128.163.1.77"

and for /26 your script did

# NET ---
# NET 128.163.252.192/26
# WARN - option missing a value (router-discovery)
# WARN - option missing a value (mask-supplier)
Dhcp Server 1.1.1.1 add scope 128.163.252.192 255.255.255.192 "128.163.252.192-26-pks2-resnet-rp" ""
Dhcp Server 1.1.1.1 Scope 128.163.252.192 set optionvalue 6 STRING "128.163.3.10" "128.163.1.6"
Dhcp Server 1.1.1.1 Scope 128.163.252.192 set optionvalue 3 STRING "128.163.252.1"
Dhcp Server 1.1.1.1 Scope 128.163.252.192 set optionvalue 51 STRING "345600"
Dhcp Server 1.1.1.1 Scope 128.163.252.192 set optionvalue 44 STRING "128.163.2.77" "128.163.1.77"


For /22 your script did

# NET 172.24.156.0/22
# WARN - option missing a value (router-discovery)
# WARN - option missing a value (mask-supplier)
Dhcp Server 1.1.1.1 add scope 172.24.156.0 255.255.252.0 "172.24.156.0-22-ah-280-rp" ""
Dhcp Server 1.1.1.1 Scope 172.24.156.0 set optionvalue 6 STRING "128.163.3.10" "128.163.1.6"
Dhcp Server 1.1.1.1 Scope 172.24.156.0 set optionvalue 3 STRING "172.24.156.1"
Dhcp Server 1.1.1.1 Scope 172.24.156.0 set optionvalue 51 STRING "345600"
Dhcp Server 1.1.1.1 Scope 172.24.156.0 set optionvalue 44 STRING "128.163.2.77" "128.163.1.77"


0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34894157
Sorry, but I'm a little confused.  Is the output from my script wrong for the non /24  cases?  If so, what is the output supposed to be?  Or is it OK?
0
 
LVL 1

Author Comment

by:richsark
ID: 34894162
Also, I forgot to answer your question:

"I guess I don't really understand where the ".1" comes from.  Is this  the gateway address?"

Yes, its the gateway address

So for:
172.24.164.64/27

Number of Usable Addresses:       30
Network Address:       172.24.164.64
Broadcast Address:       172.24.164.95
First Host Address:       172.24.164.65
Last Host Address:       172.24.164.94
First Host Address:       172.24.164.65

For Router it would be 172.24.164.65

I used this online tool to confirm
http://www.gregthatcher.com/Papers/IT/SubnetCalculator.aspx
0
 
LVL 1

Author Comment

by:richsark
ID: 34894168
For /24 looks good as well as /23

Looks like /26 is off

Original:

# NET 128.163.252.192/26
# WARN - option missing a value (router-discovery)
# WARN - option missing a value (mask-supplier)
Dhcp Server 1.1.1.1 add scope 128.163.252.192 255.255.255.192 "128.163.252.192-26-pks2-resnet-rp" ""
Dhcp Server 1.1.1.1 Scope 128.163.252.192 set optionvalue 6 STRING "128.163.3.10" "128.163.1.6"
Dhcp Server 1.1.1.1 Scope 128.163.252.192 set optionvalue 3 STRING "128.163.252.1"
Dhcp Server 1.1.1.1 Scope 128.163.252.192 set optionvalue 51 STRING "345600"
Dhcp Server 1.1.1.1 Scope 128.163.252.192 set optionvalue 44 STRING "128.163.2.77" "128.163.1.77"


Should be

# NET 128.163.252.192/26
# WARN - option missing a value (router-discovery)
# WARN - option missing a value (mask-supplier)
Dhcp Server 1.1.1.1 add scope 128.163.252.192 255.255.255.192 "128.163.252.192-26-pks2-resnet-rp" ""
Dhcp Server 1.1.1.1 Scope 128.163.252.192 set optionvalue 6 STRING "128.163.3.10" "128.163.1.6"
Dhcp Server 1.1.1.1 Scope 128.163.252.192 set optionvalue 3 STRING "128.163.252.193"
Dhcp Server 1.1.1.1 Scope 128.163.252.192 set optionvalue 51 STRING "345600"
Dhcp Server 1.1.1.1 Scope 128.163.252.192 set optionvalue 44 STRING "128.163.2.77" "128.163.1.77"
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34894174
What this version does (instead of hard-coded ".1") is to grab the last (fourth) number and numerically add 1 to it.

The remaining thing to verify is whether this is the right thing to do for the short addresses (/22, for example).

while ( <> )
{
	if ( /\# NET ([0-9]+\.[0-9]+\.[0-9]+)\.([0-9]+)/ ) { $net = $1; $lastNum = $2+1;  }
	else
	{
		s/set optionvalue 3 STRING \"0.0.0.0\"/set optionvalue 3 STRING \"$net\.$lastNum\"/;
	}
	print $_;
} 

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 34894196
Cool, looks good !

/22 looks good as well. Would you think this version is fail safe? I mean the logic is there now right?
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 34894213
I think we're good.

0
 
LVL 1

Author Closing Comment

by:richsark
ID: 34894220
Very fast response!

Good work
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now