gpt4 book ai didi

perl - 在 Perl 中从 IP 地址获取所有主机名

转载 作者:行者123 更新时间:2023-12-01 15:03:51 25 4
gpt4 key购买 nike

我正在尝试找到一种方法来获取解析为 IP 地址的所有主机名。

gethostbyaddr 函数似乎只从 DNS 中检索第一条记录(无论它是在标量上下文还是列表上下文中)。

例子:

my $hostname = gethostbyaddr(inet_aton($ip_to_check), AF_INET);
$print($hostname); //output: joe.example.com

my @hostnames = gethostbyaddr(inet_aton($ip_to_check), AF_INET);
foreach my $hostname (@hostnames){
print "(", join(',',@hostnames), ")"; //output: (joe.example.com,,2,4,?)
}

从终端:

$ host 192.168.1.5
5.1.168.192.in-addr.arpa domain name pointer joe.example.com.
5.1.168.192.in-addr.arpa domain name pointer john.example.com.

我听说 Net::DNS 更强大一些,但我还没有运气让它也能提取所有条目。

最佳答案

我结合使用了此处和堆栈溢出中其他地方给出的答案来找到我正在寻找的答案。

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

# change IP from 192.168.1.15 to 15.1.168.192.in-addr.arpa for searching
my $target_IP = join('.', reverse split(/\./, $ip_to_check)).".in-addr.arpa";

# query DNS
my $query = $res->query("$target_IP", "PTR");

# if a result is found
if ($query){
print("Resolves to:\n");

# for every result, print the IP address
foreach my $rr ($query->answer){
# show all unless the type is PTR (pointer to a canonical name)
next unless $rr->type eq "PTR";

# remove the period at the end
printf(substr($rr->rdatastr, 0, -1));
}
}

关于perl - 在 Perl 中从 IP 地址获取所有主机名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5682421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com