Perl nslookup: Difference between revisions

From DWIKI
No edit summary
 
No edit summary
 
Line 1: Line 1:
 
{{Snippet_desc|name=nslookup|description=like the command, returns the ip of a hostname (if any) or undef|lang=perl}}
<enscript lang=perl>
<enscript lang=perl>
sub nslookup {
sub nslookup {

Latest revision as of 11:10, 29 June 2007

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>