Link to home
Start Free TrialLog in
Avatar of ReneGe
ReneGeFlag for Canada

asked on

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
SOLUTION
Avatar of Chris Dent
Chris Dent
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
Avatar of ReneGe

ASKER

Thanks Chris,

I'll look at it tomorrow.

Cheers,
Rene
Avatar of xylog
xylog

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
Avatar of ReneGe

ASKER

@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
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
Avatar of ReneGe

ASKER

Correction:

On the D.C.

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

Chris
Avatar of ReneGe

ASKER

@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

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
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.
Avatar of ReneGe

ASKER

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
Avatar of ReneGe

ASKER

for /f %i in ('netsh dhcp server show scope ^|findstr "255.0"') do netsh dhcp server scope %i show clients
ASKER CERTIFIED SOLUTION
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
Avatar of ReneGe

ASKER

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
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.
Avatar of ReneGe

ASKER

Works for me.

Thanks a million for your help!!