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

Hi fellow Monks,
Has anyone been able to create multiple dns queries to multiple nameservers in a multi-threaded fashion? I am using the Net::DNS::Resolver module to do single queries as follows
#!/usr/bin/perl use strict; use warnings; use Net::DNS; # Create DNS Resolver object my $res = Net::DNS::Resolver->new; $res->nameservers(); # Specify the name server my $nameserver = "12.104.122.2"; #Perform the query #my $query = $res->searchlist(@host); my $query = $res->search('www.chase.com'); $res->print; if ($query) { foreach my $rr ($query->answer) { next unless $rr->type eq "A"; print "host resolves to " . $rr->address . "\n"; } } else { print "query failed: ", $res->errorstring, "\n"; }

Replies are listed 'Best First'.
Re: How to do multiple dns queries ?
by pg (Canon) on Nov 23, 2002 at 03:55 UTC
    Try what jaldhar suggested, but if you want to learn how to use thread, check out the attached code, which is a multi-thread version of your code.
    use strict; use warning; use Net::DNS; use threads; sub resolve { my $res = Net::DNS::Resolver>new; $res->nameservers(); my ($nameserver, $url) = @_; my $query = $res->search($url); $res->print; my $result; if ($query) { foreach my $rr ($query>answer) next unless $rr->type eq "A"; $result = $rr->address; } } return [$url, $result]; } my $nameserver = "12.104.122.2"; my @url = ("www.perlmonks.com", "www.foxnews.com"); my @thread_pool; foreach my $url @url { push @thread_pool, threads->new(\&resolve, $nameserver, $url); } my @result; foreach my $thread @thread_pool { push @result, $thread->join; }
      When I run the above program, I get the following error -
      Can't locate threads.pm in @INC (@INC contains: /usr/lib/perl5/5.6.0/i386-linux /usr/lib/perl5/5.6.0 /usr/lib/perl5/site_perl/5.6.0/i386-linux /usr/lib/perl5/si te_perl/5.6.0 /usr/lib/perl5/site_perl .) at ./dns2.pl line 8.
      BEGIN failed--compilation aborted at ./dns2.pl line 8.
      Is this a verion 5.6.1 thing ?
Re: How to do multiple dns queries ?
by jaldhar (Vicar) on Nov 23, 2002 at 03:30 UTC

    POE::Component::Client::DNS says it is a wrapper for non-blocking Net::DNS. It lets other tasks to run while something is waiting for a nameserver to respond, and it lets several DNS queries run in parallel.

    merlyn recently wrote about using POE in IIRC Linux Magazine. The column might be available at his website.

    --
    જલધર

      jaldhar => I thought I had to install some app called POE before I could use POE::Component::Client::DNS. Is this true?

        No POE is just another module package available from CPAN.

        --
        જલધર