Solved

req: script to insert router in this file

Posted on 2011-02-14
19
457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

626 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