in reply to Specifying a DNS resolver

So i have taken the advise of using Net::DNS. For simplicity, I have located code on the web and modified it. For the most part I get what i need. I have figured out how to do the name servers, but unfortunately, i don't think it it meets my needs. But I could be missing something. I have specified 2x nameservers. the x.x.50.50 is not online currently. Is there a way using Net:DNS, it would attempt to query the name server, and produce an "error" to the fact of no response, then go to the next nameserver, so on and so forth? If the name server is responding, I would want to display "Name Server - <IP> - followed by all the results". Below is the code I attempted. The "query failed", currently never get executed because the other nameserver is responding

use warnings; use strict; use Net::DNS; use Data::Dumper::Simple; my $dnsserver; my @resolver; #@resolver=qw[192.168.50.50, 192.168.10.98]; #foreach $dnsserver (@resolver) { #print "Validating Entries against $dnsserver\n"; my $resolver = Net::DNS::Resolver->new( nameservers =>[qw(192.168.50.50 192.168.10.98)], recurse => 0, debug => 1, ); my $ip = "192.168.50.70"; print " ##### Resolver State ##### \n"; $resolver->print; print "\n\n ##### End Resolver State ##### \n"; my $target_ip = join('.', reverse split(/\./, $ip)).".in-addr.arpa +"; my $query = $resolver->query($target_ip, 'PTR'); my $reply = $resolver->search("garytest.fedlab.local"); #warn Dumper(\$query, \$reply); if ($query) { foreach my $answer ($query->answer) { next unless $answer->type eq 'PTR'; print $answer->rdatastr, "\n"; } } else { print "query failed: ", $resolver->errorstring, "\n"; } if ($reply) { foreach my $rr ($reply->answer) { next unless $rr->type eq "A"; print $rr->address, "\n"; } } else { warn "query failed: ", $resolver->errorstring, "\n"; } #}

Replies are listed 'Best First'.
Re^2: Specifying a DNS resolver
by hippo (Archbishop) on Apr 07, 2023 at 15:01 UTC

    What you would need for that is a loop, which is the very thing in your code which has been commented out. Why?

    Sample working solution (data modified to become an SSCCE):

    use warnings; use strict; use Net::DNS; my @resolver = qw[192.168.50.50 8.8.8.8]; for my $dnsserver (@resolver) { print "Validating Entries against $dnsserver\n"; my $resolver = Net::DNS::Resolver->new( nameservers =>[$dnsserver], recurse => 1, debug => 1, ); print " ##### Resolver State ##### \n"; $resolver->print; print "\n\n ##### End Resolver State ##### \n"; my $query = $resolver->query('www.perlmonks.org', 'A'); if ($query) { foreach my $answer ($query->answer) { next unless $answer->type eq 'A'; print $answer->rdatastr, "\n"; } } else { print "query failed: ", $resolver->errorstring, "\n"; next; } }

    🦛

      I commented it out because I was not sure if that was accurate or if there was a function with Net::DNS that was available that I was not aware or able locate searching the web. If doing the loop, as you have indicated and I had in the code is the best/only solution, I am going with it. Thank you for your response and have a great day