Perl nslookup

From DWIKI
Name nslookup
Description like the command, returns the ip of a hostname (if any) or undef
Language perl

<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>