2012-02-11 2 views
3

따라서 otter book에는 주어진 호스트 이름에 대해 동일한 주소를 반환하는지 반복적으로 확인하기위한 작은 스크립트 (173 페이지 참조)가 있습니다. 그러나이 책에 제공된 솔루션은 호스트에 정적 IP 주소가있는 경우에만 작동합니다. 여러 개의 주소가 연관된 호스트와 작동하게하려면이 스크립트를 어떻게 작성합니까? 여기 perl과 Net :: DNS를 사용하는 DNS 검사

코드입니다 :

#!/usr/bin/perl 
use Data::Dumper; 
use Net::DNS; 

my $hostname = $ARGV[0]; 
# servers to check 
my @servers = qw(8.8.8.8 208.67.220.220 8.8.4.4); 

my %results; 
foreach my $server (@servers) { 
    $results{$server} 
     = lookup($hostname, $server); 
} 

my %inv = reverse %results; # invert results - it should have one key if all 
          # are the same 
if (scalar keys %inv > 1) { # if it has more than one key 
    print "The results are different:\n"; 
    print Data::Dumper->Dump([ \%results ], ['results']), "\n"; 
} 

sub lookup { 
    my ($hostname, $server) = @_; 

    my $res = new Net::DNS::Resolver; 
    $res->nameservers($server); 
    my $packet = $res->query($hostname); 

    if (!$packet) { 
     warn "$server not returning any data for $hostname!\n"; 
     return; 
    } 
    my (@results); 
    foreach my $rr ($packet->answer) { 
     push (@results, $rr->address); 
    } 
    return join(', ', sort @results); 
} 

답변

0

내가 가진 문제였습니다 나는 그런 www.google.com 같은 여러 주소, 반환 된 호스트 이름의 코드 호출이 오류를 얻고 있었다 :

*** WARNING!!! The program has attempted to call the method 
*** "address" for the following RR object: 
*** 
*** www.google.com. 86399 IN CNAME www.l.google.com. 
*** 
*** This object does not have a method "address". THIS IS A BUG 
*** IN THE CALLING SOFTWARE, which has incorrectly assumed that 
*** the object would be of a particular type. The calling 
*** software should check the type of each RR object before 
*** calling any of its methods. 
*** 
*** Net::DNS has returned undef to the caller. 

이 오류는 CNAME 유형의 rr 개체에서 주소 메서드를 호출하려고했음을 의미합니다. 나는 'A'타입의 rr 객체들에 대해서만 주소 메쏘드를 호출하려고한다. 위의 코드에서 'A'유형의 객체에 주소를 호출하는지 확인하지 않습니다. 나는이 코드 줄 (하나하지 않는 한 다음)를 추가하고, 그것을 작동 : 코드의

my (@results); 
foreach my $rr ($packet->answer) { 
    next unless $rr->type eq "A"; 
    push (@results, $rr->address); 
} 

이 줄은 RR 객체의 유형 "A"가없는 한 $packet->answer에서받은 다음 주소로 건너 뜁니다.

관련 문제