Link to home
Start Free TrialLog in
Avatar of SeaSenor
SeaSenorFlag for United States of America

asked on

Mac address and DHCP file project

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

ASKER

sorry... the ip address range was 192.168.2.1 - 192.168.2.254  
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?

Avatar of Don Thomson
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

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

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


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...
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?
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
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
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
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland 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
Aye.... tis a beautiful thing.  

Points awarded to ye on this fine St. Patricks day.
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
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