与Gethost()失败(使用NET :: hostent)



我在这里做错了什么,我不知道。该小程序应该将列出的4个IPv4地址使用,并使用hottent的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:if(我的$ 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 $namen";
}

您会注意到我已经评论了系统主机命令,当我使用它的工作正常时,但我没有想到一种捕获域的方法(并使输出保持沉默)。任何帮助都非常感谢。

使用系统(@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.

使用gethostbyaddr进行反向查找。

use Net::hostnet qw( gethostbyaddr );
use Socket       qw( inet_aton );
my $h = gethostbyaddr(inet_aton($ip)));
say $h->name;   # Not $h->$name

您的位置放错了$ sigil。

此代码:

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

...应该是:

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

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

最新更新