Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Well, I'm down to the nitty-gritty at last! :-) The script to produce a list of hosts pinged is working, but I haven't got a clue as to how to sort the list by ping time. Here's the script in its current form:

#!/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... :-) ) # Create our host list to sort from open OF, ">hostlist.txt" or die "Can't create hostlist.txt: $!"; select OF; $|++; # Unbuffer output file select STDOUT; print("Now scanning $netaddr, please wait....\n"); foreach $host (@hosts) { ($ret, $duration, $ip) = $p->ping($host, 5.5); if ($ret) { printf OF ("%s [ip: $ip] is alive %.2f ms)\n", gethostbyip($host) +, 1000 * $duration); } } close OF; $p->close(); print("Now sorting the list....\n"); open IF, "<hostlist.txt" or die "Can't open hostlist.txt for input: $! +"; my @data = <IF>; close IF; # Don't need the file anymore, so delete it #if (unlink("hostlist.txt") > 0) { # print("Couldn't remove hostlist.txt file for some reason....\n"); #} my @sorted = map {s/(^|\D)0+(\d)(?=\t)/$1$2/g; $_} sort map {s/(\d+)(?=\t)/sprintf"%03.3d",$1/ge; $_} @data; print(@sorted); sub gethostbyip { use Socket; my $hostip = $_[0]; my $iaddr = inet_aton($hostip); my $hostname = gethostbyaddr($iaddr, AF_INET); $hostname = $hostip unless defined $hostname; 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 You *could* put in only a single IP address, but there wouldn't be much point to that, now would there? :-) EO_USAGE exit; }
The "my @sorted" section there I copied as a placeholder from another script that was in one of the messages here, so I'd know the basic formula/idea. However, I don't know what that stuff should be changed to in order to get the sorting order I want. (Oh yea, and the "if" that's checking on the unlink to get rid of the file isn't working, either - the file unlinks fine but I always get the message. *grin*)

The script provides output in the format of: [ip: 111.222.333.444] is alive 70.05 ms)
What I need is to sort that output by that last value there - the latency time. I don't suppose there's a sort wizard here, who can tell me what the sort in my script needs to be changed to, so that it works as needed? :-)


In reply to How to sort output? by dguntner

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    [karlgoethebier]: thao4: Si tout le reste échoue demande en francais ;-) There are some French speaking monks in the monastry...
    [erix]: weird: a dev version perl (perl-5.27.9) is released, but git doesn't contain a perl-5.27.10 yet
    [karlgoethebier]: ...not me ;-)

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (11)
    As of 2018-02-21 10:04 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (277 votes). Check out past polls.