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!
- Standard input/output within C++ using _popen and _pclose does not work. Output is blank
- Ruby doesn't work...
ruby -e "puts ``netsh dhcp...
"` gives me nothing - Python doesn't work...
import subprocess, subprocess.call('netsh dhcp...')
gives me nothing - 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.
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.
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 usingShellExecute
, something likeShellExecute(0, "open", "cmd.exe", "/C *yourCommand* > output.txt", 0, SW_HIDE);