#!/usr/bin/perl

use strict;

die <<"ENDUSAGE" if ($#ARGV < 0);

Usage: xr-is-live HOST [HOST...]

Polls stated host(s) for live status. Exits with the number of unreachable
hosts.

Sample usage:
  xr-is-live onehost        - checks if the one host is down
  xr-is-live h1 h2 h3 h3 h5 - checks if this network is down (this can be
                              assumed when exit status is larger than 3)

ENDUSAGE

for my $h (@ARGV) {
    next if fork();
    if (!testlive($h)) {
	print ("$h is not reachable\n");
	exit (1);
    }
    exit (0);
}
my $ret = 0;
while (1) {
    last if (wait() == -1);
    $ret++ if ($?);
}

print ("total $ret not reachable host(s)\n") if ($ret);
exit ($ret);

sub testlive($) {
    my $h = shift;

    system("ping -c3 -t1 '$h' >/dev/null") and return undef;
    return 1;
}
    
