我正在尝试获取主机的所有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.1
和2.2.2.2
?
答案 0 :(得分:8)
您可以使用Net::DNS::Resolver来获取主机名的IPv4地址(A记录):
use warnings;
use strict;
use feature 'say';
use Net::DNS::Resolver;
my $res = Net::DNS::Resolver->new;
my $name = 'stackoverflow.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';
}
}
输出:
stackoverflow.com has the following IPv4 addresses:
151.101.65.69
151.101.193.69
151.101.1.69
151.101.129.69
答案 1 :(得分:0)
简单的纯 perl 打印域 stackoverflow.com
的 IP :
use Socket;
print join'.',unpack('C4',inet_aton('stackoverflow.com'));
print "\n";