We talked about this in the CB a bit. I'll just re-iterate what I said there:

After talking to you some more,. it seems that you do want at least one file output at the end. I suggest you go through, ping everything (once each!), while writing to your log file. At the same time, push stuff onto an array so you can pretty-print it out later.

Update: Here's one possible way to go about going it. Do also look at Clownburner's suggestion, as well -- it's a little more complicated, but probably a lot faster. Anyways, this code will spit out two output files, as well as telling you, in real-time, which hosts are up. Note the $SIG{INT} handler so you can drop out early if you want to. The command line options are just icing.

#!/usr/bin/perl -w use strict; use Net::Ping; use Getopt::Std; $|++; my %opts; getopts('?ht:s:',\%opts); if ($opts{'h'} or $opts{'?'}) { print "Usage:\n"; print "\t$0 [-t timeout] [-s subnet]\n"; exit; } $opts{'t'} ||= 5; unless ($opts{'s'}) { print "Enter subnet: "; chomp($opts{'s'} = <STDIN>); } $opts{'s'} =~ s/^(\d+)\.(\d+)\.(\d+).*/$1.$2.$3/ or die "Bad subnet fo +rmat: use xxx.xxx.xxx"; my (@up,@down); my @ips = map {"$opts{'s'}.$_"} 1..254; my $ping = Net::Ping->new("icmp",$opts{'t'}); $SIG{INT} = \&cleanup; for (@ips) { if ($ping->ping($_)) { print "Host $_ is UP\n"; push @up, $_; } else { print "Host $_ is DOWN\n"; push @down, $_; } } cleanup(); sub cleanup { print "Cleaning up and writing output files.."; open UP_FILE, ">ips_online" or die "Can't create online ip file: $!" +; print UP_FILE map {"$_\n"} @up; close UP_FILE or die "Can't close online ip file: $!"; open DOWN_FILE, ">ips_offline" or die "Can't create offline ip file: + $!"; print DOWN_FILE map {"$_\n"} @down; close DOWN_FILE or die "Can't close offline ip file: $!"; print "Done.\n"; exit; }

 
perl -e 'print "I love $^X$\"$]!$/"#$&V"+@( NO CARRIER'


In reply to Re: Pingger Script4 by Chmrr
in thread Pingger Script4 by muaddib2

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.