Greetings, Oh Most Monkly of Monks! :-)

My quest to produce a script which will give me a sorted list of hosts pinged is coming along nicely, thanks to some people here who pointed me in the right direction to find the information I needed to get started. All was going along pretty well, until I decided to Get Clever and have it return the hostname of an IP being pinged. That's when it Got Interesting. :-) Now when I try to run the script, I get:

Use of uninitialized value in printf at ./pingit.pl line 33.

Line 33 is the printf line which says the host is alive. Here's the script so far:

#!/usr/bin/perl -w # # Ping a range of IP addresses, and list sorted by ping time. # # This uses the ICMP echo method instead of UDP echo, as # some routers don't pass UDP echo. Also, if the remote host # doesn't have an appropriate service listening on the UDP # echo port, it won't answer. # # D. Guntner, 21-July-2007 # Ver 1.0, 21-July-2007 use Net::Ping; use Net::Netmask; die "I need root privs to run, dude.\n" unless $> == 0; # Get the IP address(es) my $netaddr = shift(@ARGV); usage() unless $netaddr; # Give usage message if no input my $hostname=""; my $block = new Net::Netmask($netaddr); my @hosts = $block->enumerate(); my $p = Net::Ping->new("icmp"); $p->hires(); # Comment out this line if no Time::HiRes installed # (Or better yet, install Time::HiRes... :-) ) foreach $host (@hosts) { ($ret, $duration, $ip) = $p->ping($host, 5.5); if ($ret) { printf("%s [ip: $ip] is alive %.2f ms)\n", gethostbyip($host), 10 +00 * $duration); } } $p->close(); sub gethostbyip { use Socket; my $hostip = @_ ; my $iaddr = inet_aton($hostip); my $hostname = gethostbyaddr($iaddr, AF_INET); return $hostname; } sub usage { use File::Basename; my $progname = basename($0); print <<"EO_USAGE"; This script will ping scan a range if IP addresses, and return a list sorted by ping time. Give address in CIDR format. Usage: $progname {IP/NETMASK} Example: $progname 1.2.3.4/24 You *could* put in only a single IP address, but there wouldn't be much point to that, now would there? :-) EO_USAGE exit; }
No, it's not actually doing the sort yet. :-) I'm still making sure it pings all hosts and returns the right values. I *could* do without the host names if I had to, but it would be nice to have them. I'm at a loss as to what value is uninitialized, however. It doesn't seem to matter if I put "1.2.3.4" as an address or "1.2.3.4/24" (just as an example - I'm not actually using those as input when I run this) as one; the error message keeps coming up. Anyone know what it might be talking about?

--Dave


In reply to Getting "uninitialized value" that I can't figure out... by dguntner

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.