获取主机名的所有 IP 地址

Getting all IP addresses of a hostname

我正在尝试获取主机的所有 IP 地址。

这是 nslookup 输出:

>>nslookup site.com

Server:         8.8.8.8
Address:        8.8.8.8#53

Non-authoritative answer:
Name:   site.com
Address: 1.1.1.1
Name:   site.com
Address: 2.2.2.2

我试过这段代码:

use Socket;
use Data::Dumper;
my $name = "site.com";
@addresses = gethostbyname($name)   or die "Can't resolve $name: $!\n";
@addresses = map { inet_ntoa($_) } @addresses[4 .. $#addresses];
print Dumper(\@addresses);

这是输出:

['1.1.1.1'];

要同时获得 1.1.1.12.2.2.2 吗?

您可以使用 Net::DNS::Resolver 获取主机名的 IPv4 地址(A 记录):

use warnings;
use strict;

use feature 'say';

use Net::DNS::Resolver;

my $res = Net::DNS::Resolver->new;

my $name = 'whosebug.com';
my $q = $res->query($name);

if ($q){
    print "$name has the following IPv4 addresses:\n";
    for ($q->answer){
        say $_->address if $_->type eq 'A';
    }
}

输出:

whosebug.com has the following IPv4 addresses:
151.101.65.69
151.101.193.69
151.101.1.69
151.101.129.69

简单的纯 perl 打印域的 IP whosebug.com :

use Socket;
print join'.',unpack('C4',inet_aton('whosebug.com'));
print "\n";