Entries tagged netzwerkkarte aktiv

Pruefen ob ein Computer online ist mit ping und perl

Posted on 18. Mai 2012 Comments

Ich habe bei der Arbeit ein kleines Perl-Skript geschrieben, welches prüfen sollte, ob bestimmte Computer in bestimmten Räumen(also IP-Ranges) noch online sind oder schon automatisch ausgestellt wurden. Dieses Skript sendet ein Paket über ping und guckt dann, ob es ein packetloss gibt. Im Grunde wäre das in der Bash besser gewesen, aber so kann man das, wenn man es ein bisschen anpasst, auch unter anderen OS verwenden.

Code

# 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;
}

Download