blackrose has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible i can capture whether my 'mnblookup' call to a client Windows PC succeeded or failed ? I have this line of code -
open (NMBLOOKUP, "|nmblookup -r -T $client"); select NMBLOOKUP; close NMBLOOKUP;
where $client is the NETBIOS name of the PC on the network. I've tried using $? after the 'close' statement like so - my $exit = $?; but i can't get it to return anything other than zero, regardless of whether 'nmblookup' gets a positive or negative response. (It'll get a negative response if the PC isn't active for example). This tells me i'm not capturing the exit status of the process i want to capture, only that the above code itself has successfully executed. Anyone got any experience of this ?

Edit by tye: add CODE tags

Replies are listed 'Best First'.
Re: capturing exit status of a process
by gellyfish (Monsignor) on Aug 06, 2004 at 11:22 UTC

    You actually want to examine the return value of the close() which has the exit code of the child program (or rather the return value of wait(3) but in this case it is the same.)

    /J\

Re: capturing exit status of a process
by Jaap (Curate) on Aug 06, 2004 at 10:41 UTC
    did you try system() or exec()?
    ### returncode 0 is success, 1+ is failure my $returnCode = system("nmblookup -r -T $client"); print "returnCode: $returnCode\n";
      many thanks Jaap. I tried your example but i'm still getting $returnCode = 0 even though 'nmblookup' reports that it could not contact $client. Am i just reporting that 'nmblookup' itself succeeded here, not that the result of the nmblookup was a failure ?
Re: capturing exit status of a process
by hmerrill (Friar) on Aug 06, 2004 at 11:11 UTC
    The Perl Cookbook p.559 has recipe 16.4 "Reading or Writing to Another Program" where it describes using the pipe at the beginning to write to the program.
    $pid = open(WRITEME, "| program arguments") or die "Couldn't fork: +$!\n"; print WRITEME "data\n"; close(WRITEME) or die "Couldn't close: $!\n";
    The only difference I can see is that the Perl Cookbooks example *includes* error handling ("open(...) or die ...")on the open and close - I think that's what you are looking for to tell you if it succeeded or failed.

    HTH.

      grateful thanks for the response. What this seems to do, is verify that executing the process 'nmblookup' was indeed successful and not the fact that the result of the nmblookup was a failure. I've tried many permutations of where to capture the exit status but the only time i get anything other than 0, is if i try something that Perl doesn't like and the code itself fails.