Get output of "netsh dhcp server ..." command

4.3k views Asked by At

This may sound simple, but I've hit my head against the wall for weeks about it. I have a C++ program that needs to get the output of the following Windows command line command:

netsh dhcp server 192.168.200.15 scope 192.168.200.0 show clients 1

Go ahead and try that, replacing 192.168.200.15 with your DHCP server (in my case it's a Windows 2003 server -- I'm guessing your DHCP server must be Windows to work with this command) and 192.168.200.0 with the subnet you're on. It produces a good list of MAC addresses, IP addresses, hostnames, etc. Now I need to get that output!

  1. Standard input/output within C++ using _popen and _pclose does not work. Output is blank
  2. Ruby doesn't work... ruby -e "puts ``netsh dhcp..."` gives me nothing
  3. Python doesn't work... import subprocess, subprocess.call('netsh dhcp...') gives me nothing
  4. The command in a batch file, appended with " > SomeFile.txt" works if I run from command line, but not if ANY of these methods call the batch file to run, even with the "/start" switch. The output text file is filled with my good data if I execute the batch file manually, but not if a program calls it. So frustrating.

I'm pretty sure I've tried more methods, these are just the tools I can remember. My system is Windows 7 Professional 64-bit. Probably doesn't matter, because something about "netsh dhcp server ..." commands make the output go ... somewhere? I've looked at STDOUT and STDERR. Other netsh commands are fine, such as "netsh show helper" but communication with a DHCP server makes this text unobtainable.

I've even gone to the lengths of running a Wireshark capture to see if maybe I could take the route of sniffing for the data I want off the wire.
All packets showed up right after executing the command This is the second page... You can see it goes into RPC stuff

So it starts out with LDAP stuff, then RPC stuff, all of which I cannot seem to extract the information I need, nor can I understand the RPC protocol. But all I want is the output of a command! I feel I shouldn't have to go to such great lengths!

If anyone has any ideas on how to just get the output of this freaking command, even just to a file, I am ALL EARS. I can use programming to get the output of a file easily.

3

There are 3 answers

1
MeNa On

Some options:

1) This command requires administrator rights, Are you sure that you run the program as admin?

2) You are using _popen that cannot both read and write. try using ShellExecute, something like ShellExecute(0, "open", "cmd.exe", "/C *yourCommand* > output.txt", 0, SW_HIDE);

2
Pekka On

The DHCP Server API is comprehensively exposed in PowerShell - this means there are a bunch of .Net classes available that you can wrap into either COM or native DLL functions that you can call from your C++ application is you are prepared to load a .NET runtime into the process.

You could, of course, decompile the wrappers to see how they get to the data and follow copy the same native path to bypass the CLR stack.

0
EricM On

Extremely late but just in case some else finds this... You may need to run netsh add helper dhcpmon.dll on the workstation.