Link to home
Start Free TrialLog in
Avatar of richsark
richsarkFlag for United States of America

asked on

req: script to insert router in this file

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
Avatar of sjklein42
sjklein42
Flag of United States of America image

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

Avatar of richsark

ASKER

Hi,

So would I create a file.pl and my file

so I would run as

add-router.pl dhcp.txt > router-add.conf
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
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"
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

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

Cool, awesome,

So in a case were it maybe other then /24, it will compensate?
Hi Buddy,

I came across a /27 and /26 and it did not work for that? Can you see please :)
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?

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

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"


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

Cool, looks good !

/22 looks good as well. Would you think this version is fail safe? I mean the logic is there now right?
ASKER CERTIFIED SOLUTION
Avatar of sjklein42
sjklein42
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Very fast response!

Good work