?
Solved

Batch File: Monitor DHCP activities

Posted on 2011-04-28
16
Medium Priority
?
1,001 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:ReneGe
  • 8
  • 4
  • 4
16 Comments
 
LVL 71

Assisted Solution

by:Chris Dent
Chris Dent earned 200 total points
ID: 35486527
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
 
LVL 10

Author Comment

by:ReneGe
ID: 35487829
Thanks Chris,

I'll look at it tomorrow.

Cheers,
Rene
0
 
LVL 5

Expert Comment

by:xylog
ID: 35493903
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
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
LVL 10

Author Comment

by:ReneGe
ID: 35494159
@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
 
LVL 71

Expert Comment

by:Chris Dent
ID: 35494183
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
 
LVL 10

Author Comment

by:ReneGe
ID: 35494214
Correction:

On the D.C.

>server show scope  =  LISTED ALL DHCP SCOPS BUT NOT THEIR CONTENTS
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 35494224
The command is literal, show scope shows you the configured scopes, not leases or reservations within the scope.

Chris
0
 
LVL 10

Author Comment

by:ReneGe
ID: 35494246
@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
 
LVL 71

Expert Comment

by:Chris Dent
ID: 35494278

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
 
LVL 5

Expert Comment

by:xylog
ID: 35494373
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
 
LVL 10

Author Comment

by:ReneGe
ID: 35494446
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
 
LVL 10

Author Comment

by:ReneGe
ID: 35494453
for /f %i in ('netsh dhcp server show scope ^|findstr "255.0"') do netsh dhcp server scope %i show clients
0
 
LVL 5

Accepted Solution

by:
xylog earned 1800 total points
ID: 35494498
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
 
LVL 10

Author Comment

by:ReneGe
ID: 35494645
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
 
LVL 5

Expert Comment

by:xylog
ID: 35495505
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
 
LVL 10

Author Comment

by:ReneGe
ID: 35515218
Works for me.

Thanks a million for your help!!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The top devops trends for 2017 are focused on improved deployment frequency, decreased lead time for change and decreased MTTR.
Your business may be under attack from a silent enemy that is hard to detect. It works stealthily in the shadows to access and exploit your critical business information, sensitive confidential data and intellectual property, for commercial gain. T…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

840 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