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

<readme> Is there a way to suppress output? In other words, perl newbie is running through a file doing an nslookup in DOS – performing an nslookup on ip addresses to get the hostname. My sysadmin (at work)does not approve acitive state to be installed on workstation - will only let me use perl.exe w/ perl56.dll, so I can't use perl modules. Would appreciate any help. Code looks like this…</readme>
@nsl = `nslookup $_`; $reso = $nsl[3]; if ($? = 0) { $getit = (split (/\s+/, $reso))[1]; # pull hostname only print “$_ -> $getit\n”; } else { print “$_ - could not resolve.\n”; }
<readme> The output looks real good if it successfully resolves ip to hostname, but the problem comes in if it can’t resolve – my output looks crappy – like this… </readme>
***dc1 can’t find server name for address 9.0.1.2: Non-existent domain 1.2.3.4 -> Hostname1 5.6.7.8 -> Hostname2 9.0.1.2 -> Could not resolve. 3.4.5.6 -> Hostname7 ***dc1 can’t find server name for address 7.8.9.0: Non-existent domain 7.8.9.0 -> Could not resolve.
<readme> Is there a way to suppress all the ***Can’t find server name… crap out of my output and keep the output pretty? It seems pretty simple but I can’t get my hands on it. Thanks. </readme>

Replies are listed 'Best First'.
Re: Suppress output from nslookup
by Necos (Friar) on May 18, 2007 at 02:47 UTC
    Well, perl has the gethostby* functions, and in your case, you need gethostbyaddr(). From the perldoc page:

    For the gethost*() functions, if the "h_errno" variable is sup-ported in C, it will be returned to you via $? if the function call fails. The @addrs value returned by a successful call is a list of the raw addresses returned by the corresponding sys- tem library call. In the Internet domain, each address is four bytes long and you can unpack it by saying something like:  ($a,$b,$c,$d) = unpack('C4',$addr[0]);

    The Socket library makes this slightly easier:
    use Socket; $iaddr = inet_aton("127.1"); # or whatever address $name = gethostbyaddr($iaddr, AF_INET); # or going the other way $straddr = inet_ntoa($iaddr);


    This should get you going in the right direction.
    Theodore Charles III
    Network Administrator
    Los Angeles Senior High
    4650 W. Olympic Blvd.
    Los Angeles, CA 90019
    323-937-3210 ext. 224
    email->secon_kun@hotmail.com
    perl -e "map{print++$_}split//,Mdbnr;"
Re: Suppress output from nslookup
by Anonymous Monk on May 18, 2007 at 04:59 UTC
    As Necos suggested is better...but if you're having problems dealing with that..it looks as though you're getting standard error output (STDERR) from nslookup intermixing with your script output. Therefore, throw away the STDERR output for the nslookup:
    @nsl = `nslookup $_ 2>nul:`; #for windows @nsl = `nslookup $_ 2>/dev/null`; #for unix/linux