如何在 Unix/Linux 上使用 perl 执行批量 DNS 查询?

发布于 2024-12-09 13:53:04 字数 205 浏览 1 评论 0原文

我有一个正在尝试获取 IP 地址的系统列表。我已成功使用 Perl 的 Net::DNS 模块对单个主机名执行 IP 地址查询。然而,我有 1,000 个系统需要 IP 地址。

有没有办法通过一次查询获取所有这些 IP 地址?

如果没有,是否有办法获取整个 DNS 条目列表(例如单个域)?如果我知道了,那么我可以简单地将其放入哈希中并以这种方式引用 IP 地址。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天冷不及心凉 2024-12-16 13:53:04

无需定制 Perl。这可以使用 dig-f 选项(BIND 工具的一部分)来完成:

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

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
守不住的情 2024-12-16 13:53:04

对于大型域数据集,这将快速完成,而不需要真正解析结果; IP 始终位于 $results{$domain}[0][4] 中。这不是单个查询,但它们将同时完成(IIRC 一次最多进行 10 个查询),因此会很快完成。只需确保 DNS 服务器运营商在短时间内处理这么多请求不会出现问题。

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;

输出:

$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'
                            ]
                          ]
        };

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'
                            ]
                          ]
        };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文