因此,在otter book中,有一个小脚本(参见第 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 );
}