3

tl;dr - Cannot capture output from commands that use RPC to a remote server using any programming language I try, even though these commands output back to the command prompt when typed manually

I've got a function that uses fpipe to issue the netsh command to display stats from our DHCP server. I do this same fpipe buffer-reading technique elsewhere in my program and it works fine, but here I'm getting no output at all:

EDIT: I'm simplifying my code to a more proof-of-concept type snippet. It's actually an all-encompassed .cpp source file that still replicates the issue.

#include <iostream>
#include <string>
#include <stdio.h>  // for _popen() and _pclose()
using namespace std;

    int main()
    {
        char buff[512];
        buff[0]=0;
        string cmd="netsh dhcp server 192.168.200.15 scope 192.168.200.0 show clients 1";
        FILE *fpipe = _popen(cmd.c_str(),"r");
        if(fpipe==NULL) cout<<"Failed to open"<<endl; else cout<<"Opened pipe successfully";
        while(fgets(buff,sizeof(buff),fpipe)!=NULL) cout<<buff<<endl;
        _pclose(fpipe);
    }

The only output I'm getting is:

Opened pipe successfully

But if I copy/paste that command into another command prompt window, it runs fine and spits out many lines immediately.

So where does it go? Why does nothing ever go into my buffer?

Also, changing the "cmd" string from the netsh to the "dir" command will successfully output dir's output.

EDIT: Even system(cmd.c_str()); gives me no output!

EDIT: Curiously, cmd="netsh dump"; actually outputs fine, but still cmd="netsh dhcp server... does not. Something weird with the way CMD routes netsh text or... something? Outside of my comfort zone here.

EDIT: This is obviously an issue outside of C++ because when I'm in a Python 2.7 shell (this is Windows 7 64-bit, by the way), I try this:

>>> import subprocess
>>> subprocess.call('dir', shell=True)
 Volume in drive C has no label.
 Volume Serial Number is 2E2B-B34F

 Directory of C:\MyDir

09/30/2013 01:24 PM  <DIR>       .
09/30/2013 01:24 PM  <DIR>       ..
09/20/2013 10:11 AM          45  test.txt
             1 File(s)       45 bytes
             2 Dir(s)   162,260,246,528 bytes free
0
>>> subprocess.call('netsh dhcp server 192.168.200.15 scope 192.168.200.0 show clients 1', shell=True)
0
>>>
armani
  • 93
  • 1
  • 10
  • 23
  • Here's somethings to check.Does netsh need root privileges? And do you run your program with the appropriate privileges? User? Permissions? What about replacing netsh with /path/netsh? Does your PATH and LIBPATH environments setup correctly for the cmd? – Bitdiot Sep 27 '13 at 17:39
  • No, I can run netsh normally with a non-administrative cmd prompt, and run the C++ program in an elevated cmd prompt anyway. If the path variables weren't set up correctly, I would've seen the error output in either STDOUT or STDERR... plus also the other netsh command (netsh dump) did output normally. Just "netsh dhcp..." isn't working. – armani Sep 27 '13 at 21:30
  • does `string cmd="netsh dhcp server 192.168.200.15 scope "+scopes[x]+" show clients 1 > out.txt"` produce output in the corresponding file? – t7bdh3hdhb Sep 30 '13 at 09:09
  • @t7bdh3hdhb The out.txt file gets created, but it's empty. Zero bytes in size. – armani Sep 30 '13 at 13:08

4 Answers4

1

Maybe the output is going to STDERR and not to STDOUT?

Try this at the end of your system-command -> " 2>&1".

For example: FILE *p = popen("g++ main.cpp -o main", "r");

Try this: FILE *p = popen("g++ main.cpp -o main 2>&1", "r");

SoulfreezerXP
  • 459
  • 1
  • 5
  • 19
0

Try replacing your _popen with:

FILE *fpipe = _popen(cmd.c_str(),"rt");

Maybe you still have to specify the read-mode completely.

And also use error checking to check whether _popen has worked:

if(fpipe == NULL)
    cout<<"Failed to open"<<endl;
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
  • No change in the output. I don't get the "Failed to open" message either, so it looks like the fpipe variable gets something. Maybe just the blanks outputted for formatting, but not the netsh data that is retrieved from the DHCP server via RPC to be displayed in the command window. – armani Sep 24 '13 at 13:01
0

Could it be a privilege problem? If the netsh dhcp ... command requires admin privilege to work properly, maybe the problem is that the application does not inherit admin privilege when it runs and thus it cannot complete the command successfully. Of course, it seems like it should output something to STDERR in that case, but I don't know...

  • I can run the netsh command in a normal non-administrator user account's CMD prompt just fine. Also, I usually compile and run the C++ program with an elevated command prompt anyway. So that's not it. – armani Sep 27 '13 at 21:28
0

Are you sure about the command? I am assuming you are using Windows , as there were mentions of "command prompt" when I do a netsh /? I dont see a dhcp sub command at all. I see only dhcpclient . Even with this wrong command ( I Suppose ) , I get an output that the sub command is not available . Here is the code, I use Cygwin. hence _p* functions are replaced with p* functions.

#include <iostream>
#include <vector>
#include <string>
using std::cout;
using std::endl;
using std::string;
using std::vector;

#include <stdio.h>

int main(int argc, char* argv[])
{
    cout<<"Entered DHCPLookup() function"<<endl;
    vector<string> scopes;
    scopes.push_back("192.168.16.0");
    scopes.push_back("192.168.144.0");
    // some scopes omitted here
    scopes.push_back("192.168.155.0");
    scopes.push_back("192.168.200.0");
    char buff[512];
    for(int x=0;x<scopes.size();x++)
    {
        buff[0]=0;
        string cmd="netsh dhcp server 192.168.200.15 scope "+scopes[x]+" show clients 1";
        cout<<cmd<<endl;
        FILE *fpipe = popen(cmd.c_str(),"r");
        while(fgets(buff,sizeof(buff),fpipe)!=NULL) cout<<buff<<endl;
        pclose(fpipe);
    }
}

Here is the output

Entered DHCPLookup() function netsh dhcp server 192.168.200.15 scope 192.168.16.0 show clients 1 The following command was not found: dhcp server 192.168.200.15 scope 192.168.16.0 show clients 1.

netsh dhcp server 192.168.200.15 scope 192.168.144.0 show clients 1 The following command was not found: dhcp server 192.168.200.15 scope 192.168.144.0 show clients 1.

netsh dhcp server 192.168.200.15 scope 192.168.155.0 show clients 1 The following command was not found: dhcp server 192.168.200.15 scope 192.168.155.0 show clients 1.

netsh dhcp server 192.168.200.15 scope 192.168.200.0 show clients 1 The following command was not found: dhcp server 192.168.200.15 scope 192.168.200.0 show clients 1.

a_secenthusiast
  • 319
  • 3
  • 12
  • The command is definitely fine; As mentioned, when I copy/paste the command into a separate command prompt window, it executes fine and fills my screen with DHCP client information. You probably don't have a DHCP server on your network with IP 192.168.200.15, so that's probably why you're getting that error. – armani Sep 30 '13 at 13:05
  • Agreed!But can you take a look at the program , it is able to execute the command and print the output as well. – a_secenthusiast Sep 30 '13 at 14:56
  • I just tried it here and got the same output (that is, no output except for seeing which command it's on via "cout< – armani Sep 30 '13 at 16:52