0

I'm cycling through the responses I'm getting from issuing the "nslookup [IP]" command at a shell, using the C++ _popen pipe magic.

As you may know (try it from a terminal... Windows command prompt may output differently than another OS; I'm using Windows 7), an nslookup query will return something like:

C:\MyApps>nslookup 8.8.8.8
Server:  dns.mydomain.com
Address:  192.168.200.15

Name:    google-public-dns-a.google.com
Address:  8.8.8.8

Here's my code (the important snippet):

vector<string> IPAddresses;
// [...] some code to populate IP Addresses into that vector [...]

char buff[512];
for(int x=0;x<IPAddresses.size();x++)
{
    cmd = "nslookup " + IPAddresses[x];
    FILE *fpipe = _popen(cmd.c_str(),"r");
    while(fgets(buff, sizeof(buff), fpipe)!=NULL)
    {
        //DEBUG CODE HERE
    }
}

Now check my "DEBUG CODE" examples and their outputs (noting that the "cannot find IP: Non-existent domain" error is normal when there's no DNS record present):

if(buff[0]=='N') cout<<buff;

Output:

Name:   computer1.mydomain.com
Name:   computer2.mydomain.com
*** dns.mydomain.com can't find 192.168.200.55: Non-existent domain
Name:   computer3.mydomain.com
*** dns.mydomain.com can't find 192.168.200.122: Non-existent domain

Debug code 2:

if(buff[0]=='*') cout<<buff;

Output:

*** dns.mydomain.com can't find 192.168.200.55: Non-existent domain
*** dns.mydomain.com can't find 192.168.200.122: Non-existent domain

How can the non-existent domain errors pop up when I'm looking for buff[0] to be 'N'? In fact, it's showing up in both debug examples, so my program thinks that char is both 'N' and '*'???

4

1 回答 1

3

这两个字符串可能不是因为您的代码正在打印它而被输出,而是因为nslookup正在将它们写入stderr您的程序没有捕获(因此与您的输出混合)。

于 2013-07-24T15:42:03.393 回答