#!/bin/sh
# -*- perl -*-
# This code allows us to start perl from our path or an environment variable
# rather than hardcoding a path into the #! line.  It works from sh or csh.
(exit $?0) && eval 'exec ${QPERLQ-perl} -x $0 ${1+"$@"}'
if (! $?QPERLQ) setenv QPERLQ perl
exec $QPERLQ -x $0 $argv:q

#!/usr/local/bin/perl -w
#
# $Id: timedping,v 1.2 1997/09/21 13:47:34 ejb Exp $
# $Source: /home/ejb/scripts/RCS/timedping,v $
# $Author: ejb $
#

require 5.002;
use strict;

my $whoami = ($0 =~ m,([^/\\]*)$,) ? $1 : $0;
#my $dirname = ($0 =~ m,(.*)[/\\][^/\\]+$,) ? $1 : ".";

&usage() unless (@ARGV >= 1);
my $host = undef;
my $quiet = 0;
my $timeout = 30;
while (@ARGV)
{
    my $arg = shift(@ARGV);
    if ($arg eq "-q")
    {
	$quiet = 1;
    }
    elsif ($arg eq "-timeout")
    {
	&usage() unless scalar(@ARGV);
	$timeout = shift(@ARGV);
    }
    elsif ($arg =~ m/^-/)
    {
	&usage();
    }
    else
    {
	&usage() if defined $host;
	$host = $arg;
    }
}
&usage() if (scalar(@ARGV) || (! defined $host));
print "Ping $host at ", scalar(gmtime), "\n";
my $quiet_flag = ($quiet ? "-q" : "");

my $pid = open(PING, "ping $quiet_flag $host|") or
    die "$whoami: running ping failed: $!\n";
$SIG{'ALRM'} = $SIG{'INT'} = sub { kill 2, $pid };
alarm $timeout;
while (<PING>)
{
    next if m/^\s*$/;
    print;
}
print "\n";
close(PING);

sub usage
{
    die "Usage: $whoami host [ -q ] [ -timeout timeout ]\n";
}
