#!/usr/bin/perl
# file:   online-ping.pl
# author: repat - <repat@repat.de> - http://repat.de
# date:   Feb 2012
# brief:  Checks if computers are online by pinging

# The string containsPacketLoss is searching for
$packetloss = "100% packet loss";
# Init
$returnval = "";

print "\nCheck if Computers are online...\n";

# Scan IP-Range
for (my $i = 1; $i <=255; $i++) {
		# c = one package, w = deadline, q = quiet
        $ipcmd = "ping -c1 -q -w1 169.254.1." . $i;
        # execute and retreive output
        $returnval = `$ipcmd`;
        if (containsPacketLoss() == 0) {
            # output
            print "Computer  with IP 169.254.1." . $i . "is still online.\n";
            # write log file
			system("echo Computer with IP 169.254.1.". $i . localtime(time) . " ONLINE >> /home/user/log");
        }
}

sub containsPacketLoss(){
	# check by RegEx if the string specified above is in the returnvalue
	# of the ping command
	if ($returnval =~ m/$packetloss/ ){
                return 1;
        }
        return 0;
}

