mellin has asked for the wisdom of the Perl Monks concerning the following question:

I'm using the Resolver class from Net::DNS and it seems to take very long time to ask IP addresses for selected domain. Our DNS is working correctly and hosting six IP addresses for it's domain. It takes about 5 to 6 seconds for the script to return output, and this is not really desired since i was intending to output the answer through website.

Ignore the lame validity checks it performs, they are not meant to be included like that later on. I'm only curious if there's a way i can speed up things, maybe even change the module form Net::DNS to something other?


#!/usr/bin/perl -w use strict; use Net::DNS; my @domainIp = resolveDomainIp ('test.ad.local'); my $i=1; foreach (@domainIp) { print $i++ . ": $_\n"; } sub resolveDomainIp { my $adFqdn = shift; error ("i need to know the active directory domain name (eg. microsoft +.ad.local)", 'exit') unless ($adFqdn); error ("provided active directory domain doesn't look like a domain na +me", 'exit') unless ($adFqdn =~ /^([a-zA-Z0-9]+)\.([a-zA-Z0-9]+)/); my $res = Net::DNS::Resolver->new; my $query = $res->search($adFqdn); + my @dcIp; if ($query) { foreach my $rr ($query->answer) { next unless $rr->type eq "A"; push @dcIp, $rr->address . " (record type " . $rr->type . ")"; } return @dcIp; } else { my $errMsg = $res->errorstring; error ("dns server the platform is asking from doesn't know any ip a +ddresses for $adFqdn", 'exit') if @_ != 1; } }

Replies are listed 'Best First'.
Re: Net::DNS::Resolver and very slow reply
by McDarren (Abbot) on Sep 04, 2006 at 10:29 UTC
    "Our DNS is working correctly.."

    Are you sure?
    I ran your script on my local machine against 'google.com', and got the following:

    time perl resolver.pl 1: 64.233.187.99 (record type A) 2: 64.233.167.99 (record type A) 3: 72.14.207.99 (record type A) real 0m0.661s user 0m0.530s sys 0m0.040s

    I tried it against a few other domains with similar results. Are you sure you don't have some local DNS weirdness going on?

    What happens when you do manual lookups against your local domain?

    Cheers,
    Darren :)

      Yes, i'm pretty sure there's nothing wrong with it. When using Nslookup from Command Prompt (i'm on windows) and querying against our own domain, it takes about 0.05 seconds to answer back. When querying the same domain with this script, it takes that five to six seconds.

      I have the same problem from home as well, with totally different network and using my ISP's DNS server. I've even tested this script on Windows XP and OS X, so i'm really confused here if you get it to answer in a fractions of a second.

        mmm, okay.
        Well, just for the sake of the exercise, I re-wrote your script and simplified it a bit. I also changed it so that it would take a domain name to test from the command line. Here is my version:
        #!/usr/bin/perl -w use strict; use Net::DNS; my $domain = shift or die "Please supply a domain to query\n"; my @ips = resolve($domain); my $i; for (@ips) { print ++$i . ": $_\n"; } sub resolve { my $domain = shift; my $res = Net::DNS::Resolver->new; my $query = $res->search($domain); my @results; if ($query) { foreach my $rr ($query->answer) { next if $rr->type ne "A"; push @results, $rr->address . " (record type " . $rr->type + . ")"; } return @results; } else { return $res->errorstring; } }

        I get similar results with this as I did with your original, but perhaps you can try this and see if it makes any difference. Some sample output:

        time perl resolver.pl cnn.com 1: 64.236.24.12 (record type A) 2: 64.236.24.20 (record type A) 3: 64.236.24.28 (record type A) 4: 64.236.29.120 (record type A) 5: 64.236.16.20 (record type A) 6: 64.236.16.52 (record type A) 7: 64.236.16.84 (record type A) 8: 64.236.16.116 (record type A) real 0m0.562s user 0m0.320s sys 0m0.010s

        As you can see, still quite fast. I also tried it on a windoze box and it returns just as quickly.

        Hope this helps somewhat,
        Darren :)

        Using an Windows XP on dial connection made this times:
        C:\Documents and Settings\mda>perl dns.pl 11 wallclock secs ( 0.00 usr + 0.03 sys = 0.03 CPU)1: 200.228.126.26 + (record type A)
        The changes has:
        use Benchmark; my $td = new Benchmark; my @domainIp = resolveDomainIp ('hostname'); print timestr(timediff(new Benchmark, $td));

        But a question... Why nslookup is more quickly?
        I donīt known details, but supose the use of caches on windows, or more simple do identify nameserver...

        When I defining my nameserver has a less time. Checking why, I see other nameserver (local connection... lost configs ?)...

        #.. cut .. my $res = Net::DNS::Resolver->new; print "PRE: ", $res->nameservers, "\n"; $res->nameservers('my.desired.ip.addr'); print "POS: ", $res->nameservers, "\n"; #.. cut ..
        Note:

        --
        Marco Antonio
        Rio-PM