#!/usr/local/bin/perl5 -w use IPC::Open3; use Symbol; # .ping contains hostnames, each on a seperate line. open(HOSTS, "<.ping") || die("Can't open hostsfile! ($!)\n"); @hosts = ; close(HOSTS); %PING = (); %STATUS = ( -1 => "Host not found", 0 => "Not done", 1 => "Host is alive", 2 => "No reply from host", ); foreach $host (@hosts) { chomp $host; $WTR = gensym(); $RDR = gensym(); $ERR = gensym(); $PING{$host}{'PID'} = open3($WTR, $RDR, $ERR, "/sbin/ping -n -q -c 1 $host"); close($WTR); while (<$RDR>) { $PING{$host}{'OUT'} .= $_; } while (<$ERR>) { $PING{$host}{'ERR'} .= $_; } $PING{$host}{'STATUS'} = 0; } foreach $host (keys %PING) { if(defined($PING{$host}{'ERR'})) { if($PING{$host}{'ERR'} =~ /Unknown host/) { $PING{$host}{'STATUS'} = -1; next; } } if($PING{$host}{'OUT'} =~ /.*, (.*) packets received.*/) { $PING{$host}{'STATUS'} = ($1 == 1?1:2); } } foreach $host (keys %PING) { printf "%-30s %s\n", $host, $STATUS{$PING{$host}{'STATUS'}}; }