I have the following code (It probably needs changed too, but I'm learning). I want to ping a server, check to see if the server is down, and if it is, say so (I am going to call an external program later, but right now I just want to print it). My thought was to ping the server, pipe the output to a text file and look for the word "Out" as in "Request Timed Out". Here's what I have so far. It looks like my text is getting into the array, but I am having problems with testing it. I sure would appreciate some assistance:
#!/perl
use strict;
use warnings;
use vars qw/ $device @raw_data $line /;
# $device = shift or die;
$device = "localhost"; # here I put a device or IP to check..
`ping -n 1 $device > $device.txt`;
open(FILE, "$device.txt") || die("Could not open file!");
@raw_data = <FILE>;
close(FILE);
foreach $line (@raw_data)
{
chomp($line);
if ($line = "out") {
print("$device failed ping test");
}
}
# `del $device.txt`;
Start Free Trial