Batch File: Monitor DHCP activities

Hi there,

I need to create a batch file to monitor our Windows Server 2003 DHCP server, and all changes are to be displayed within the DOS window.

Thanks for your help,
Rene
LVL 10
ReneGeAsked:
Who is Participating?
 
xylogConnect With a Mentor Commented:
OK the problem is you need to either use IP address or use \\ in front of the name. I was using IP address so I did not see it when I tried it ...

for /f %i in ('netsh dhcp server \\MYDHCPSERVERNAME show scope ^|findstr "255.0"') do netsh dhcp server \\MYDHCPSERVERNAME scope %i show clients

for /f %i in ('netsh dhcp server [MYDHCPSERVERIP] show scope ^|findstr "255.0"') do netsh dhcp server [MYDHCPSERVERIP] scope %i show clients
0
 
Chris DentConnect With a Mentor PowerShell DeveloperCommented:
You get even less choice with DHCP.

You can use NetSh, hmm let me see:

netsh dhcp server <ServerIP> scope <ScopeIP> show clients
netsh dhcp server <ServerIP> scope <ScopeIP> show reservedip

It's not a very pleasant interface, but the alternatives are poor. It's netsh, or the DHCP API. If it's any help I have a PowerShell script, of sorts, that can pull clients from DHCP.

http://poshcode.org/2310

It's almost entirely C#, working with the APIs is pretty heavy going.

Chris
0
 
ReneGeAuthor Commented:
Thanks Chris,

I'll look at it tomorrow.

Cheers,
Rene
0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
xylogCommented:
Here is a way to dump all the assigned DHCP ip addresses for all of your scopes with a single command from the command prompt:

for /f %i in ('netsh dhcp server MYDHCPSERVERNAME show scope ^|findstr "255.0"') do netsh dhcp server MYDHCPSERVERNAME scope %i show clients

Please note if you want to use this in a batch file you need to use two % not just one so it would be:

show-dhcp-clients.bat
================
for /f %%i in ('netsh dhcp server MYDHCPSERVERNAME show scope ^|findstr "255.0"') do netsh dhcp server MYDHCPSERVERNAME scope %%i show clients
0
 
ReneGeAuthor Commented:
@xylog,

The command line did not work for me.

So I tried: netsh dhcp server mydhcpservername show scope

And I got the an error saying that the command was not found: netsh ... scope

So I openned a DOS window on my XP computer:
Typed:
>netsh
>dhcp  = Command not found.

I openned a DOS window on my Domain Controller
Typed:
>netsh
>dhcp
>server MYDHCPSERVERNAME  =  COMMAND NOT FOUND
>show scope  =  LISTED ALL DHCP SCOPS BUT NOT THEIR CONTENTS

Thanks and cheers,
Rene
0
 
Chris DentPowerShell DeveloperCommented:
Which OS version on each? I tend to test stuff on Win 7 / 2008 R2 which can mangle results as MS do so like to change things, including netsh.

Anyway, with the command above, you'd need to select the scope, then list the clients inside that scope. I've been meaning to write a decent PowerShell interface for DHCP, but time seems limited recently.

Chris
0
 
ReneGeAuthor Commented:
Correction:

On the D.C.

>server show scope  =  LISTED ALL DHCP SCOPS BUT NOT THEIR CONTENTS
0
 
Chris DentPowerShell DeveloperCommented:
The command is literal, show scope shows you the configured scopes, not leases or reservations within the scope.

Chris
0
 
ReneGeAuthor Commented:
@Chris-Dent

XP Pro and the D.C. is W.S. 2003

A command line would be preferable. I'll only use Power Shell if absolutly needed.

Thanks for your help,
Rene
0
 
Chris DentPowerShell DeveloperCommented:

NetSh is all you'll get really, it'll be a case of playing with the options inside. You're looking to enter the Scope context, then get the clients. I'm afraid I can't give you 2003 / XP specific syntax, I don't run either any more.

But, from where you are, it should be a case of:

scope <NetworkAddressOfYourScope>

Then take a look at the show options.

It's not a very good or consistent interface I'm afraid, but it really is the only one you'll get unless you go for exporting to a file in the DHCP MMC (if it even permits that).

Chris
0
 
xylogCommented:
Did you try the command I posted above:

for /f %i in ('netsh dhcp server MYDHCPSERVERNAME show scope ^|findstr "255.0"') do netsh dhcp server MYDHCPSERVERNAME scope %i show clients

That will dump out all client leases for all scopes.
0
 
ReneGeAuthor Commented:
Yes I've tried it.
It's the MYDHCPSERVERNAME portion that bugs on my D.C.

(Yes I replace MYDHCPSERVERNAME with the name of my DHCP server  ;-)

So since you insisted, I'v played with it, removed it from the command line as:
for /f %i in ('netsh dhcp server show scope ^|findstr "255.0"') do netsh dhcp server scope %i show clients

This worked when running it on the domain controller (that is also the DNCP server) but now, I need to run it from my XP Pro computer.

Thanks
0
 
ReneGeAuthor Commented:
for /f %i in ('netsh dhcp server show scope ^|findstr "255.0"') do netsh dhcp server scope %i show clients
0
 
ReneGeAuthor Commented:
Works on a remote W.S. 2003.

And what about the fact that the NETSH DHCP command is not available on my XP Pro PC?

Have any ideas?

Thanks,
Rene
0
 
xylogCommented:
You can use psexec to run the command remotely:

psexec -U username \\MYDHCPSERVERNAME netsh dhcp server \\MYDHCPSERVERNAME show scope

What used to do was run a batch file as a scheduled task on the dhcp server every 15 minutes to dump all the client info to a text file that I could view on a webpage.
0
 
ReneGeAuthor Commented:
Works for me.

Thanks a million for your help!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.