#!/usr/bin/perl ###################################################################### # pingtest.pl - (C) 2012 James Nonnemaker - james (AT) nonnemaker.us # # This program is merely to demonstrate my perl coding skills/style. # This program reads a lis of IP addresses from a local file and checks # to see if each host is online. If a host is offline, it emails the # administrator for that host, as specified in the local file. # # While this program does work, it is only for demonstration purposes. # If you require an actual program similar to this one, do contact me. # ####################################################################### use Net::Ping; $date = localtime(); open($fh, '<:encoding(UTF-8)', '/tmp/hosts.txt') or die "Could not open file $!"; while ($line = <$fh>) { chomp $line; @linesplit = split(/:/, $line); print "Checking $linesplit[0] for $linesplit[1] ... "; $p = Net::Ping->new("icmp"); if ($p->ping($linesplit[0], 2)) { print "Host is up.\n"; } else { print "Oh no! Host is DOWN!!! Alerting user.\n"; open(MAIL, "|/usr/sbin/sendmail -t"); print MAIL "To: $linesplit[1]\n"; print MAIL "From: root\n"; print MAIL "Subject: Host is DOWN - $linesplit[0]\n\n"; print MAIL "Hello $linesplit[1],\n\nWe tried to ping $linesplit[0] on $date, but it did not respond. It seems to be down. Please investigate."; close(MAIL); } }