3

そのため、カワウソの本には小さなスクリプト (173 ページを参照) があり、その目的は、DNS サーバーを繰り返しチェックして、特定のホスト名に対して同じアドレスを返しているかどうかを確認することです。ただし、本に記載されている解決策は、ホストが静的 IP アドレスを持っている場合にのみ機能します。複数のアドレスが関連付けられているホストで動作するようにするには、このスクリプトをどのように記述すればよいでしょうか?

コードは次のとおりです。

#!/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 );
}
4

1 に答える 1

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 オブジェクトで address メソッドを呼び出そうとしたことを意味していました。タイプ「A」のrrオブジェクトでのみaddressメソッドを呼び出したいです。上記のコードでは、タイプ「A」のオブジェクトでアドレスを呼び出していることを確認するためのチェックがありません。私はこのコード行を追加しました(1つでない限り次)、それは動作します:

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

$packet->answerこのコード行は、rr オブジェクトのタイプが「A」でない限り、取得した次のアドレスにスキップします。

于 2012-02-15T19:03:25.190 に答える