gnu@perl has asked for the wisdom of the Perl Monks concerning the following question:

I am having an odd problem with Net::Ping. Certain addresses passed to it are causing errors.
substr outside of string at /usr/local/lib/perl5/5.6.1/sun4-solaris/Ne +t/Ping.pm line 401. Use of uninitialized value in unpack at /usr/local/lib/perl5/5.6.1/sun +4-solaris/Net/Ping.pm line 401. Use of uninitialized value in numeric eq (==) at /usr/local/lib/perl5/ +5.6.1/sun4-solaris/Net/Ping.pm line 406.

I get this every time I pass in the IP address of the box running the code. So far I have been able to run correctly on every IP I have tried. If I pass it the loopback IP or the local IP it errors. I have moved the code to another box and set it up to use the local IP address of that box and it worked fine.

The box on which it works is a 5.8.0 installation and the one where it dosen't is a 5.6.1. I would attribute the problem to that normally, but I'm not sure. The Net::Ping is exactly the same. The new box didn't even have it so I just copied the directory from my box where it failed to the test box and all works fine.

As you can see, I commented out the section where it reads the IP addresses from a file for testing, the problem happens either way.

Could someone shed some light on what may be going on? Here is the code I'm using:

#!/usr/bin/perl -w use strict; use IO::Handle; use Net::Ping; use Net::SNMP; use Data::Dumper; my $source_file = (shift || "/tmp/ping_list.txt"); my %pid_to_host; my %host_result; my @hosts; my $OID = '.1.3.6.1.4.1.15102'; #open(IPS,"$source_file") # or die "Cannot open source file $source_file: $!\n"; autoflush STDOUT 1; #for (<IPS>) #{ # chomp; # s/ *//; # s/\cM//; # if ($_ =~ m/^\d+\.\d+\.\d+\.\d+$/) # { # push(@hosts,$_); # } # else # { # print BAD "$_ : NOT A VALID IP ADDRESS\n"; # } #} $hosts[0] = '10.51.8.109'; $hosts[1] = '127.0.0.1'; for (@hosts) { wait_for_a_kid() if keys %pid_to_host > 10; if ( my $pid = fork ) { # parent $pid_to_host{$pid} = $_; } else { # child exit ping_a_host($_); } } 1 while wait_for_a_kid(); sub ping_a_host { my $host = shift; my $p = Net::Ping->new('icmp',2); print "yes\n" if $p->ping($host) ? 1 : 0; #`/usr/sbin/ping -n $host 1 2>/dev/null` =~ /no answer from/ ? +0 : 1; } sub wait_for_a_kid { my $pid = wait; return 0 if $pid < 0; my $host = delete $pid_to_host{$pid} or warn("Why did I see $pid ($?)\n"), next; print "send_trap('ncool1','public','162',$host,$OID)\n" if (!$?) +; 1; } sub send_trap { my ($host, $community, $port, $ipAddr, $OID) = (@_); my ($session, $error) = Net::SNMP->session( -hostname => shift || $host, -community => shift || $community, -port => shift || $port ); if (!defined($session)) { exit 1; } my $result = $session->trap( -enterprise => $OID, -generictrap => 2, -specifictrap => 9999, -varbindlist => [$OID,OCTET_STRING,$ipAddr] ); if (!defined($result)) { $session->close; exit 1; } $session->close; exit 0; }

Replies are listed 'Best First'.
Re: Net::Ping Problem with local IP
by Marza (Vicar) on Feb 21, 2003 at 00:26 UTC

    If it works, then my guess is the Net-Ping might have been updated. You could see about updating it. Then again, is there any reason, you can't boost the "problem" box to 5.8.0? Other then it being a .0 release?

      Well, what concerns me is I don't know where the problem is coming from, it can't be from Net::Ping because that is a direct copy from my problem box. So it must be somewhere else in Perl.

      The majority of our production boxes are 5.6.1 releases and the upgrade to another version of perl is exceptionally long.