[gull] Comment obtenir les DNS?

Marc SCHAEFER schaefer at alphanet.ch
Fri Nov 3 10:03:34 CET 2006


On Thu, Nov 02, 2006 at 09:33:09PM +0100, Félix Hauri wrote:
> $ host -t NS -v google.com |
>     perl -pe 'printf "DNS servers for domain %s:\n",$1 if /^Trying (".*")$/;
>               printf "  DNS%d: %s = %s\n", $c++ + 1, $1, $2  if 
>                       /^(.*)\.\s+\d+\s+IN\s+A\s+(.*)$/;
>               undef $_;'

J'aime bien le shell,
mais cependant
il y a des façons plus belles,
d'écrire autrement:

#! /usr/bin/perl -w
# BUGS
#    - error reporting weak

use strict;

use Net::DNS;  # man Net::DNS

my $resolver = new Net::DNS::Resolver;

my %answers;
foreach my $domain (@ARGV) {
   my $packet = $resolver->query($domain, 'NS');
   if (defined($packet)) {
      my @names;
      foreach my $rr ($packet->answer) {
         if (ref($rr) eq 'Net::DNS::RR::NS') {
            push(@names, $rr->nsdname);
         }
      }
      $answers{$domain} = \@names;
   }
}

# NOTES
#    - Two presentations: either the NS alone, one per line;
#      or, if multiple questions, an extended format.
if (scalar(keys %answers) == 1) {
   my @values = values %answers;
   print join("\n", @{$values[0]}), "\n";
}
else {
   foreach my $domain (keys %answers) {
      print $domain,
            ': ',
            join(', ', @{$answers{$domain}}), "\n";
   }
}

Exemples:
   schaefer at reliant:/tmp$ ./perl-dns.pl linux-gull.ch
   ns2.nimag.net
   government.linux-gull.ch

   schaefer at reliant:/tmp$ ./perl-dns.pl linux-gull.ch admin.ch   
   linux-gull.ch: government.linux-gull.ch, ns2.nimag.net
   admin.ch: ins2.admin.ch, ins3.admin.ch, ins1.admin.ch

-- 
Je lis les messages bien formatés. N'abusez pas du Cc:. Texte == efficace.
Citer n'est pas concaténer. Editez vos messages, ça gagne du temps.
Marc se met au blog `-o ro': http://www.alphanet.ch/schaefer_chronique.html



More information about the gull mailing list