*BSD News Article 72773


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!munnari.OZ.AU!news.ecn.uoknor.edu!news.ysu.edu!odin.oar.net!malgudi.oar.net!rclnews.eng.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!uwm.edu!news-res.gsl.net!news.gsl.net!news.mathworks.com!newsfeed.internetmci.com!newsfeeder.servtech.com!murphy2.servtech.com!news
From: sanjay <sanjay@servtech.com>
Newsgroups: comp.sys.sun.admin,alt.sys.sun,comp.unix.bsd.misc,comp.unix.programmer,comp.unix.bsd.bsdi.misc,comp.unix.admin
Subject: Re: ping script
Date: Wed, 03 Jul 1996 21:16:44 -0400
Organization: ServiceTech, Inc.
Lines: 81
Message-ID: <31DB1B7C.38CD450E@servtech.com>
References: <Pine.SOL.3.93.960702171209.4062B-100000@gold.acns.fsu.edu>
NNTP-Posting-Host: smog.roc.servtech.com
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="------------40DFC53C226FBCD85DDDF94E"
X-Mailer: Mozilla 2.0 (X11; I; Linux 1.2.8 i586)
To: "Douglass E. Davis" <davis@gold.acns.fsu.edu>
Xref: euryale.cc.adfa.oz.au comp.sys.sun.admin:69949 alt.sys.sun:10363 comp.unix.bsd.misc:1205 comp.unix.programmer:39084 comp.unix.bsd.bsdi.misc:4223 comp.unix.admin:44449

This is a multi-part message in MIME format.

--------------40DFC53C226FBCD85DDDF94E
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Douglass E. Davis wrote:
> 
> I am trying to make a script that will ping a computer every few minutes
> and run a program if there is no answer.
> 
> I was thinking about making a C program to do the same thing. it would
> just do a :
> system("ping blazay > datafile ")
> then it would read datafile  to see if the ping worked.
> 
> But if some one has a script or C program  that could do this I would
> appreciate the source, or if you could just tell me how to do it.

Here is a bash script that will do what you want. The syntax is:

	ping_script [-t time] <host> <program> [arg] ...

It will execute <program> with all the arguments [arg] ... if ping
to the <host> is unsuccessful at the interval of "time" minutes. The
default "time" is 1 minute when you do not supply the -t option.
Hope this is useful.

Sanjay

--------------40DFC53C226FBCD85DDDF94E
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="tt"

#!/bin/bash

COM=${0##*/}
USAGE="$COM [-t time] <hostname> <prog> [arg] ..."
ERR="err: usage - $USAGE"

declare -i TIME=60
while getopts t: OPT > /dev/null 2>&1 
do 
	case $OPT in 
		(t)
			TIME=$OPTARG
			TIME=$TIME*60
			;; 

		(\?)
			echo "$ERR"
			exit 1
			;; 
	esac 
done 
shift `expr $OPTIND - 1` 

if [ $# -lt 2 ]
then
	echo "$ERR"
	exit 1
fi

HOST=$1
shift 1
PROG="$*"

while true
do
	sleep $TIME
	ping -c 1 $HOST > /dev/null 2>&1; RES=$?

	if [ $RES -ne 0 ]
	then
		$PROG
	fi
done

--------------40DFC53C226FBCD85DDDF94E--