Maybe this is some use:
# Modules
use Net:

ing;
# Programming rules
# use strict;
use warnings;
# Main code
open (FILE, "<ip.txt"

or die "can not open file"; # open file with IP adresses
my @list = <FILE>; # declare and read the file in an array
close FILE; # close filehandle
my $p = Net:

ing->new("icmp"

; # declare $p and use it for ICMP ping
my %ping_totals;
my %ping_received;
my %ping_loss;
foreach (@list) # run through the array
{
chomp($_); # remove newline character
if ($_ ne 0) # if $_ has an ip declared continue
{
if ($p->ping($_)) # if ping = succesful
{
$ping_totals{$_}++; # add a ping to the total of pings for that IP addres
$ping_received{$_}++; # add a ping to the total of received pings for that IP addres
print "$_ is reachable\n";
}
else
{
$ping_totals{$_}++; # add a ping to the total of pings for that IP addres
$ping_loss{$_}++; # add a ping to the total of pings lost for that IP addres
print "$_ not reachable\n";
}
}
}
&Pause(); # small pause in program
# printing the results
&Message("The ping statistics\n\n"

;
&Message("IP address\tSent\tReceived\tLost\tUptime\n\n"

;
foreach (sort keys %ping_totals) # for each pair in %ping_totals, sort the keys, run through the hash
{
print "$_ \t $ping_totals{$_}"; # Print ping totals the tabs are not inplace more IF CLAUSES!!
if (defined($ping_received{$_}))
{
print "\t$ping_received{$_}"; # Print pings received
}
if (defined($ping_loss{$_})) # Print pings received
{
print "\t$ping_loss{$_}";
}
my $uptime = &Calc_Uptime ($_);
printf "\t\t\t %d %% \n", $uptime; # Calculate uptime
}
# closing connections / handles
$p->close(); # close ICMP object
#################################################################################################
# Library of subroutines #
#################################################################################################
sub Pause {
# Takes no parameter, but asks the user to press
# enter en then it continues, returns always true
# unless error
print "Press <enter> to continue to the results...\n";
my $continue = <STDIN>;
}
sub Message {
# Takes any string between quotes as the paramater and prints
# the string out onto the command line
my $message = shift @_;
print $message;
}
sub Countdown
{
# Takes a number of seconds (int) it should wait before continuing
# the program
my($timer) = @_;
my $timenow = time();
my $endtime = $timenow + $timer;
while (time() < $endtime)
{
$remaining = $endtime - time();
#print sprintf("$bs%-9s", GetDuration($remaining));
print "$remaining seconds remaining \n";
sleep 1;
}
}
sub Check_Exist {
# Takes the $_ which holds the IP address as parameter
# and returns:
# 0 IF no pings were either received or lost (not going to happen unless error)
# 1 IF pings were received AND lost for this IP
# 2 IF pings were received BUT NOT lost for this IP
# 3 IF pings were lost BUT NOT received for this IP
my $ip = shift @_;
my $r_exist = $ping_received{$ip};
my $l_exist = $ping_loss{$ip};
my $exists;
if (defined($r_exist) && defined($l_exist))
{
$exists = 1;
}
else
{
if(defined($r_exist) && !defined($l_exist))
{
$exists=2;
}
else
{
if(defined($l_exist) && !defined($r_exist))
{
$exists=3;
}
else
{
$exists=0;
}
}
}
return $exists;
}
sub Calc_Uptime {
# Takes the $_ which holds the IP address as parameter
# calls the subroutine Check_Exist to find out if the
# variables have been set in the hashes and returns the
# calculated uptime
use Switch;
my $ip = shift @_;
my $calc;
my $received = $ping_received{$ip}; # Get THEM FROM OTHER FUNCTION?
my $loss = $ping_loss{$ip}; # Get THEM FROM OTHER FUNCTION?
my $casevar = &Check_Exist($ip); # Call sub with variable
switch ($casevar) {
case 0 { $calc = "N/A"; return $calc; }
case 1 { $calc = ($received / $loss); return $calc;}
case 2 { $calc = "100"; return $calc; }
case 3 { $calc = "0"; return $calc; }
else { print "Error evaluating switch statement in Sub Calc_Uptime" }
}
}
It is not the most beautiful piece of code but it pings, stores and displays results (May need some tweaking but you get the idea). I think you can use and modify this for your own use...
- Raenius
"Free will...is an illusion"