문제

I am doing something wrong here and I don't know what. This small program is supposed to take the 4 IPv4 addresses listed and use hostent's gethost() to resolve the Domain. If it fails, it keeps the IPv4 format.

Output:

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

Then, the error I'm receiving:

 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 ( my $h = gethost($host) ) 19: $name = ($h->$name());

Code:

#!/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";
}

You'll notice I have commented out the system host command, when I use that it works fine but I haven't thought of a way to capture the Domain (and silence the output). Any help much appreciated.

when using system(@sysArg); I get this :

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.
도움이 되었습니까?

해결책 2

You have a misplaced $ sigil.

This code:

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

... should be:

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

Can't locate object method "137.48.78.181" hints at this: the string value of $name was being used as a method name.

다른 팁

Reverse lookups are done using gethostbyaddr.

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

my $h = gethostbyaddr(inet_aton($ip)));
say $h->name;   # Not $h->$name
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top