Mirky has asked for the wisdom of the Perl Monks concerning the following question:

I found a bit of code on "http://www.stevekallestad.com/blog/reverse_ip_lookup_using_perl.html" This does a reverse IP lookup using Perl. I'm attempting to create a Perl/Tk interface using this code but I get the following error, I'm not sure what part of this error to address, I've checked to see if $ARGv has value (it does) using print. $ip does not get value as far as I can tell, I'm stumped.
Tk::Error: Can't call method "reverse_ip" without a package or object +reference at C:\_SCRIPT\Perl\DNS\TkNMLookup.pl line 21. Tk callback for .button Tk::__ANON__ at C:/Perl/lib/Tk.pm line 252 Tk::Button::Invoke at C:/Perl/lib/Tk/Button.pm line 135 <Key-Return> (command bound to event)
### Moduels to use use Tk; use Net::IP; use Net::DNS; ### New Window my $mw = MainWindow->new; $mw->title("IP Lookup"); $mw->Label(-text => "Enter an IP Address")->pack; $mw->Entry(-textvariable => \$a)->pack; $mw->Button(-text => "Lookup", -command => \&rlookup)->pack; $mw->Entry(-textvariable => \$b)->pack; MainLoop; sub rlookup{ use Net::IP; use Net::DNS; my $ARGV = $a; # print $ARGV; my $ip = new Net::IP($ARGV[0],4); my $res = new Net::DNS::Resolver; my $answer = $res->query($ARGV->reverse_ip(),'PTR'); my $namer = $answer->{'answer'}[0]; $b = $namer->{'ptrdname'}; }
KISMIF Keep it Simple make it Fun

Replies are listed 'Best First'.
Re: Reverse IP Lookup
by ikegami (Patriarch) on Jun 08, 2007 at 15:53 UTC

    $ip is your object, not $ARGV. Change
    $ARGV->reverse_ip()
    to
    $ip->reverse_ip()

    Other problems:

    • Don't use $a, it's a special variable.
    • Don't use $b, it's a special variable.
    • Don't use $ARGV, it's an even more special variable.
    • You fetch from $ARGV[0] when you mean to fetch from $ARGV or $a.
    • You're using the dangerous indirect method call notation (new CLASS ARGS) instead of the direct methods (CLASS->new(ARGS))
    • You needlessly duplicated your use statements.
    • You don't handle the case where no names are associated with the IP address.
    • You don't handle errors.

    Fixed:

    use strict; use warnings; use Tk; use Net::IP; use Net::DNS; my $ip_field; my $name_field; my $resolver = Net::DNS::Resolver->new() sub rlookup { my $ip = Net::IP->new($ip_field, 4); my $answer = $resolver->query($ip->reverse_ip(), 'PTR'); my @names = $answer->{'answer'}; if (@names) { $name_field = $names->[0]->{'ptrdname'}; } else { $name_field = "[no associated name]"; } } { my $mw = MainWindow->new; $mw->title("IP Lookup"); $mw->Label(-text => "Enter an IP Address")->pack; $mw->Entry(-textvariable => \$a)->pack; $mw->Button(-text => "Lookup", -command => \&rlookup)->pack; $mw->Entry(-textvariable => \$b)->pack; MainLoop; }

    It still doesn't handle errors.

Re: Reverse IP Lookup
by citromatik (Curate) on Jun 08, 2007 at 15:50 UTC

    The original code from http://www.stevekallestad.com/blog/reverse_ip_lookup_using_perl.html is:

    #!/usr/bin/perl use strict; se warnings; use Net::IP; use Net::DNS; my $ip = new Net::IP($ARGV[0],4); print $ip->reverse_ip()."\n"; print "Resolving ...\n"; my $res = Net::DNS::Resolver->new; my $answer = $res->query($ip->reverse_ip(),'PTR'); my $namer = $answer->{'answer'}[0]; print "PTR Name: $namer->{'ptrdname'}\n";

    @ARGV is a special variable that contains the parameters passed to the script. In the script you copied the ip was passed as a command line option. In your case it is in the variable $a, so you must use this variable in the call to new Net::IP like this:

    my $ip = new Net::IP($a,4);

    You better don't change the @ARGV variable at all.

    BTW, when you write:

    my $ARGV = $a;

    You are assigning the scalar variable $ARGV. And when you write:

    my $ip = new Net::IP($ARGV[0],4);

    You are using the array variable @ARGV (its first element)

    citromatik