Solved

Mac address and DHCP file project

Posted on 2011-03-10
18
694 Views
Last Modified: 2012-05-11
Hi all....

I have searched around and found pieces of what i want to do, but nothing as a complete project.

Here's what I'm wanting to do:

either a batch file, or excel macro file to get the computer name, mac address and IP

I would like it to end up in an excel file( or txt file)  both for reference, and to modify for a mass DHCP reservation import into my DHCP server.

so to recap:

execute file to get computer/IP/Mac address info.
use file output to copy/paste/modify existing DHCP reservation dump file.
Import back into DHCP.

I would like to use a IP range to get the information from ... such as 192.18.2.1 - 192.168.2.254

can this be done?

thanks
0
Comment
Question by:SeaSenor
  • 11
  • 6
18 Comments
 
LVL 8

Author Comment

by:SeaSenor
Comment Utility
sorry... the ip address range was 192.168.2.1 - 192.168.2.254  
0
 
LVL 8

Author Comment

by:SeaSenor
Comment Utility
also, a question:

if a device (such as a printer, switch, access point, etc) is getting it's IP from a DHCP reservation, and the DHCP server goes down, does that device lose it's IP configuration after a certain amount of time, or does it stay with that configuration until a operational DHCP server imposes a change?

0
 
LVL 14

Expert Comment

by:Don Thomson
Comment Utility
A network device (nic, Printer etc) obtains it's ip address from a DHCP server - If that device is still on-line when the lease time is up - It just renews it. If the DHCP is offline for any reason - new IPs cannot be given out but existing ones are fine (i.e. you retart the server or Router)

To get a list of IP vs MAC address and to find out if they ar static or dynamic - use
arp -g  >IPlist.txt

Brint that txt file into excel  as a fixed width text file

Take the contents of the first column and add tracert    infront of the IP address

Now you have tracert 192.168.x.x

Export that as findname.bat  as a text file
run the following

findname > gotnames.txt

now bring both the first file iplist.txt  and the gotnames.txt into tables in access

Strip out what you don't need  Create a single table that has pc name, ip, mac and type is four fields

Once you have that you can do anything you want

0
 
LVL 8

Author Comment

by:SeaSenor
Comment Utility
thanks DTH....

I prefer not to use access at all in this scenario....just excel.


on the DHCP lease question....how long would a device keep it's IP address if it could not connect to a DHCP server?  even if the lease is up?
0
 
LVL 8

Author Comment

by:SeaSenor
Comment Utility
I tried the arp -g >IPlist.txt

i got some interesting results.

a couple of IP's I didn't know what they were, and serveral that were listed as dynamic, were actually static.
Also, I only got about 15 addresses. Where does it get it's list from?
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
In general:

Device asks for lease
DHCP server responds and gives lease for period set (e.g. 2 days).   If no DHCP server is available = no address
Half way through lease device will ask for renewal
On shutdown a device will generally release it's address so it will go back to the pool.  
At the end of lease the device will lose it's IP address if it has not been able to renew it

So you either run a redundant DHCP system or use MS DHCP with the same scope setup on different servers, but giving out different parts, e.g. for one subnet server A gives out 10.1.1.10 to 10.1.1.100 and server B gives out 10.1.1.101 to 10.1.1.200.  You add reservations into both servers, i.e.

Server a.  Range = 10.1.1.10 to 10.1.1.250.  Exclude 10.1.1.101 to 10.1.1.200 and 10.1.1.201 to 10.1.1.250
Sererv b. Range = 10.1.1.10 to 10.1.1.250.  Exclude 10.1.1.10 to 10.1.1.100 and 10.1.1.201 to 10.1.1.250

On both servers reserving, say, 10.1.1.10 or 10.1.1.150 say will give out that address for a certain MAC address and if this is an unknown MAC address then it gets one from the remaining pool.

If multiple subnets are in operation here then you would need to adjust router / switch to point to the multiple DHCP servers.

Anyway have waffled on longer there than intended so ask if want more!

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
for arp it only shows entries that you have spoken with recently.  Try:

for /l %a in (1,1,254) do ping -n 1 -w 20 192.168.1.%a
arp -a > arplist.txt

Also this only works for this subnet, the mac address for other addresses will be the router device to that subnet.

Steve
0
 
LVL 8

Author Comment

by:SeaSenor
Comment Utility
we were tossing the idea of using reservations for our switches IP config.
Would you recommend that?

The ip range i want to scan for mac's is 192.168.2.1 - 192.168.2.254

0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
I'd fix a switch's IP personally.... what happens say if your DHCP server or switch config gets broken accidentally and it can't talk to it's DHCP server...

I suppose if they were on their own VLAN / subnet and got from a different scope that had a long lease....

How many are involved here?

Just amend the IP in the for loop to ping the range:

for /l %a in (1,1,254) do ping -n 1 -w 20 192.168.2.%a
arp -a > arplist.txt

or from a batch file use %%a instead of %a

@echo off

for /l %%a in (1,1,254) do ping -n 1 -w 20 192.168.2.%%a
arp -a > arplist.txt


0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Author Comment

by:SeaSenor
Comment Utility
I assumed so.. that's why I asked the additional question. Thanks.
We are only talking about 10-12 switches. I didn't recommend it, and wanted verification of it not being a good idea to do so.

i'll try the batch file...
0
 
LVL 8

Author Comment

by:SeaSenor
Comment Utility
The batch file works fine.
It does give some odd results though...but not too bad.
What I mean is, if I specify the range of 192.168.2.%%a   it still picks up a few 192.168.3.xxx addresses.
Some say static...some say dynamic. I'm assuming that's because most are reservations in DHCP which will report as dynamic?

so.. ok...that is workable for part of the project.
Another part-  I am using a batch file that exports only the reservations of one DHCP server.....Next would be to have the batch file  (sub routine?) or even a seperate batch file...find and replace one servers IP address with another servers IP address, then import back into DHCP.

Recap:
Step 1. obtain mac addresses
Step 2. use mac addresses to form DHCP reservations (I will do manually I guess)
Step 3. export DHCP reservations from DHCPserver1
Step 4. modify exported file data to replace DHCPserver1 IP address with DHCPserver2 IP address
(I know i can use notepad-find/replace...that's what I do now)...but would like it to be automatic if possible in the batch file that exports the DHCP reservation dump.
Step 5. Import into DHCP server2

is there simple code for a batch file to find/replace text in a file?

If so, i would assume something like this for the batch file: (I would execute on the DHCPserver2)

set /p serverIP=192.168.2.1
set /p serverIP2=192.168.2.2
netsh dhcp server %serverIP% scope 192.168.0.0 dump | find /i "reservedip" > c:\DHCPReservations.txt

(whatever code goes here to find  %serverIP% and replace with %server2IP% in the DHCPReservations.txt file)

netsh exec  c:\DHCPReservations.txt

Feasible?
0
 
LVL 8

Author Comment

by:SeaSenor
Comment Utility
OK... I have this pretty much solved. Enough to satisfy me anyway :)

I created a powershell file to convert the ip addresses. It gets called from the batch file.

By using manual input from the user,  you can use this for pretty much any two DHCP servers.

Many of you could manipulate this to death and make it much better I'm sure

attached are the two files I created. Place them both on the C: drive of the importing DHCP server and run the .bat file

they aren't allowed to be attached with the extensions, so you will need to add a .ps1 extension to the ConvertDHCP file and a .bat extension to the other one....then execut the .bat file.

any suggestions/improvements are welcome....as you can see I'm pretty new at this


DHCP-reservation-Move
ConvertDHCP
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
It sounds like you have answered your own Q there.  Have been busy so unable to look at EE responses sorry.   There are plenty of ways of doing search and replace from cmd.exe prompt but can have side effects if any of a multi-tude of special characters in the results.

You shouldn't really have any special characters, though I wonder what the dump would do with, for instance, a scope name with an & | < > etc. in it (or maybe it stops you using those already in the name, can't remember!).

If you did want to loop through a file and amend within the batch you would do something like this:

@echo off
setlocal enabledelayedexpansion

set oldfile="c:\DHCPreservationEXPORT.txt"
set oldip=10.1.1.1

set newfile=c:\DHCPreservationIMPORT.txt
set newip=10.2.2.2

for /f "tokens=* delims=" %%a in ('type %oldfile%') do (
  set line=%%a
  set line= !line:%oldip%=%newip%!
  echo !line!
)> %newfile%

You could bring it all into batch, vbscript, or powershell I suppose but whatever works best for you.  You could popup a GUI inputbox for instance using a bit of VBScript:

http://scripts.dragon-it.co.uk/links/batch-gui-inputbox

or add a progress indicator as part of the loop search and replace for instance.... though I don't suppose it takes more than a fraction of a second anyway!

http://scripts.dragon-it.co.uk/links/batch-progress-indicator

There are various free search and replace third party exe's available, though I know in many corporate environments the pain of getting such as util available, officially at least, isn't worth it!

Steve
0
 
LVL 8

Author Comment

by:SeaSenor
Comment Utility
Hey Steve...thanks for the help.

I like the script, and it will keep me from having to use powershell at all, which I would prefer in this case.
However, it doesn't seem to convert the IP addresses of server1 to server2 properly.
Can you spot the issue...?

here is what I have in the .bat file:  (there's a lot of extra junk in there for fun, but I like it)


cls
@echo off
Echo.
Echo.
ECHO RUN THIS FROM THE DHCP SERVER YOU WISH TO IMPORT TO:
PAUSE
Echo.
Echo.
echo Enter the Server IP you would like to DUMP DHCP reservations from:
set /p serverIP1=
echo Enter the Server IP you would like to IMPORT DHCP reservations to:
set /p serverIP2=

netsh dhcp server %serverIP1% scope 192.168.0.0 dump | find /i "reservedip" > c:\DHCPreservationEXPORT.txt

cls
Echo.
Echo.
Echo A file is being created...please wait
call :wait

cls
Echo.
Echo.

@echo off
setlocal enabledelayedexpansion

set oldfile="c:\DHCPreservationEXPORT.txt"
set oldip=%serverIP1%

set newfile=c:\DHCPreservationIMPORT.txt
set newip=%serverIP2%

for /f "tokens=* delims=" %%a in ('type %oldfile%') do (
  set line=%%a
  set line= !line:%oldip%=%newip%!
  echo !line!
)> %newfile%



cls
Echo.
Echo.
Echo A file is being created...please wait
call :wait
cls

rem netsh exec c:\DHCPreservationIMPORT.txt

Echo.
Echo.


ECHO Finished

Echo.
Echo.


echo Woot!

Echo.
Echo.

pause

:wait
REM call this subroutine with the number of seconds to pause.
set delay=%1
if [%delay%]==[] set delay=3
set /a delay+=1
ping 127.0.0.1 -n %delay% >NUL
exit /b
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
Comment Utility
Sorry, my bad,... was just typing before didn't test.  It was just doing the last line as it was redirecting each line on top of each other.  I intended to redirect the whole for command in one go but didn't add an extra set of brackets.  Also there is an extra space in the !line:=...! line which I have removed.  Try this.

Make sure that there aren't any spaces on the end of the oldip, newip lines etc.

(for /f "tokens=* delims=" %%a in ('type %oldfile%') do (
  set line=%%a
  set line=!line:%oldip%=%newip%!
  echo !line!
))> %newfile%

So the whole thing is something like this... have added an explanation of the loop for you:

@echo off
cls
Echo.
Echo.
ECHO RUN THIS FROM THE DHCP SERVER YOU WISH TO IMPORT TO:
PAUSE
Echo.
Echo.
echo Enter the Server IP you would like to DUMP DHCP reservations from:
set /p serverIP1=
echo Enter the Server IP you would like to IMPORT DHCP reservations to:
set /p serverIP2=

netsh dhcp server %serverIP1% scope 192.168.0.0 dump | find /i "reservedip" > c:\DHCPreservationEXPORT.txt

cls
Echo.
Echo.
Echo A file is being created...please wait
call :wait

cls
Echo.
Echo.

@echo off
setlocal enabledelayedexpansion

set oldfile="c:\DHCPreservationEXPORT.txt"
set oldip=%serverIP1%

set newfile="c:\DHCPreservationIMPORT.txt"
set newip=%serverIP2%

REM This runs a for command which sets %%a to the value of each line in turn
REM This is set into the line variable so the old ip can be replaced by the new one
REM This uses Exclamation marks because delayedexpansion is turned on which is so
REM that the variable changes within the loop - it is treated as all one line
REM and evaluated only once otherwise
REM
REM Steve Knight - http://www.dragon-it.co.uk/ March 2011

(for /f "tokens=* delims=" %%a in ('type %oldfile%') do (
  set line=%%a
  set line=!line:%oldip%=%newip%!
  echo !line!
))> %newfile%

cls
Echo.
Echo.
Echo A file is being created...please wait
call :wait
cls

rem netsh exec c:\DHCPreservationIMPORT.txt

Echo.
Echo.


ECHO Finished

Echo.
Echo.


echo Woot!

Echo.
Echo.

pause

:wait
REM call this subroutine with the number of seconds to pause.
set delay=%1
if [%delay%]==[] set delay=3
set /a delay+=1
ping 127.0.0.1 -n %delay% >NUL
exit /b
0
 
LVL 8

Author Closing Comment

by:SeaSenor
Comment Utility
Aye.... tis a beautiful thing.  

Points awarded to ye on this fine St. Patricks day.
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Glad it helped! Didn't know was St Patrick's day ... they had all the parades etc. in Brum at the weekend but didn't know the actual date..

Steve
0
 
LVL 8

Author Comment

by:SeaSenor
Comment Utility
Thanks also for the link to your website.
I put some input boxes in to catch the variables of the server IP's. Makes it a little more gooey :P

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 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

14 Experts available now in Live!

Get 1:1 Help Now