in reply to Pingger Script4
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'
|
|---|