开发者

How can I perform a bulk DNS query using perl on Unix/Linux?

开发者 https://www.devze.com 2023-04-12 17:40 出处:网络
I have a list of systems for which I\'m 开发者_如何转开发trying to get IP addresses.I\'ve successfully used the Net::DNS module for perl to perform an IP address query for a single hostname.I have 1,0

I have a list of systems for which I'm 开发者_如何转开发trying to get IP addresses. I've successfully used the Net::DNS module for perl to perform an IP address query for a single hostname. I have 1,000 systems, however, that I need ip addresses for.

Is there a way to get all of these ip addresses with a single query?

If not, is there a way to get the entire DNS entry list, say, for a single domain? If I got that, then I could simply put that into a hash and reference the IP addresses that way.


No need for custom Perl. This can be done using the -f option to dig (part of the BIND tools):

$ dig -f /path/to/host-list.txt


For a large domain dataset, this'll do it fast with no real need to parse the results; an IP will always be in $results{$domain}[0][4]. It's not a single query, but they'll be done concurrently (max of 10 queries in progress at any one time IIRC) so will be done quickly. Just make sure the DNS server operator doesn't have a problem with that many requests in a short period.

use AnyEvent::DNS;
use Data::Dumper;

my @domains = qw/google.com/;
my $resolver = AnyEvent::DNS->new( server => '8.8.4.4' );
my %results;

### Set up the condvar
my $done = AE::cv;
$done->begin( sub { shift->send } );

for my $domain (@domains) {
  $done->begin;
  $resolver->resolve($domain, 'a', sub {push @{$results{$domain}}, \@_; $done->end;});
}

### Decrement the cv counter to cancel out the send declaration
$done->end;

### Wait for the resolver to perform all resolutions
$done->recv;

print Dumper \%results;

Outputs:

$VAR1 = {
          'google.com' => [
                            [
                              'google.com',
                              'a',
                              'in',
                              300,
                              '74.125.225.52'
                            ],
                            [
                              'google.com',
                              'a',
                              'in',
                              300,
                              '74.125.225.50'
                            ],
                            [
                              'google.com',
                              'a',
                              'in',
                              300,
                              '74.125.225.49'
                            ],
                            [
                              'google.com',
                              'a',
                              'in',
                              300,
                              '74.125.225.48'
                            ],
                            [
                              'google.com',
                              'a',
                              'in',
                              300,
                              '74.125.225.51'
                            ]
                          ]
        };
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号