1

我在这里做错了什么,我不知道是什么。这个小程序应该使用列出的 4 个 IPv4 地址并使用 hostent 的 gethost() 来解析域。如果失败,它将保持 IPv4 格式。

输出:

180.76.5.59 has a hostname of 180.76.5.59
199.200.9.44 has a hostname of 199.200.9.44

然后,我收到的错误:

 Can't locate object method "137.48.78.181" via package "Net::hostent" at
     ./rev.pl line 19 (#1)
 (F) You called a method correctly, and it correctly indicated a package
 functioning as a class, but that package doesn't define that particular
 method, nor does any of its base classes.  See perlobj.

Uncaught exception from user code:
Can't locate object method "137.48.78.181" via package "Net::hostent" at ./rev.pl line 19.
 at ./rev.pl line 17

17: 如果 (我的 $h = gethost($host)) 19: $name = ($h->$name());

代码:

#!/usr/bin/perl

use Modern::Perl;

use Net::hostent;
use diagnostics;

my @ipaddresses = qw/ 180.76.5.59 199.200.9.44 137.48.78.181 137.48.185.207 /;
#host 137.48.78.181
foreach my $host ( @ipaddresses )
{
  my $name = $host;

  # my @sysArg = ("host", $host);
  # system(@sysArg);

 if ( my $h = gethost($host) )
 {
  $name = ($h->$name());
 }
  print "$host has a hostname of $name\n";
}

您会注意到我已经注释掉了系统主机命令,当我使用它时它工作正常,但我还没有想到捕获域的方法(并使输出静音)。非常感谢任何帮助。

使用系统时(@sysArg);我明白了:

Host 59.5.76.180.in-addr.arpa not found: 2(SERVFAIL)
Host 44.9.200.199.in-addr.arpa. not found: 3(NXDOMAIN)
181.78.48.137.in-addr.arpa domain name pointer pc-78-181.hpr.unomaha.edu.
207.185.48.137.in-addr.arpa domain name pointer pki174b-01.ist.unomaha.edu.
4

2 回答 2

3

反向查找使用gethostbyaddr.

use Net::hostnet qw( gethostbyaddr );
use Socket       qw( inet_aton );

my $h = gethostbyaddr(inet_aton($ip)));
say $h->name;   # Not $h->$name
于 2012-11-08T23:59:18.657 回答
1

你有一个错误的 $ 印记。

这段代码:

$name = ($h->$name());  # WRONG

... 应该:

$name = ($h->name());

Can't locate object method "137.48.78.181"提示:字符串值$name被用作方法名称。

于 2012-11-09T00:00:59.840 回答