Perl nslookup

From DWIKI
Revision as of 11:07, 29 June 2007 by JYDawg (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

<enscript lang=perl> sub nslookup {

       my $name = shift;
       if( ! $name ) {
               print STDERR "nslookup: no argument given\n";
               return undef;
       }
       my @addresses;
       eval { @addresses = gethostbyname($name) };
       if( $@  ) {
               print STDERR "nslookup: failed to resolve $name -> $!\n";
               return undef;
       }
       @addresses = map { inet_ntoa($_) } @addresses[4 .. $#addresses];
       return $addresses[0];

} </enscript>