所以,在,有一个小脚本(参见第 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 );
}
我遇到的问题是,我在返回多个地址的主机名上调用代码时遇到此错误,例如 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”类型的对象上的地址。我添加了这行代码(除非是下一行),并且它有效:
my (@results);
foreach my $rr ( $packet->answer ) {
next unless $rr->type eq "A";
push ( @results, $rr->address );
}
这行代码跳到下一个地址$packet->answer
除非 rr 对象的类型是“A”。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)