*BSD News Article 74054


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!munnari.OZ.AU!news.hawaii.edu!ames!tulane.edu!cpk-news-feed4.bbnplanet.com!news.fsu.edu!gold.acns.fsu.edu!davis
From: "Douglass E. Davis" <davis@gold.acns.fsu.edu>
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, 17 Jul 1996 15:09:36 -0400
Organization: Florida State University
Lines: 128
Message-ID: <Pine.SOL.3.93.960717150307.13904B-100000@gold.acns.fsu.edu>
References: <Pine.SOL.3.93.960702171209.4062B-100000@gold.acns.fsu.edu> <4sbfat$ll@keyhole.west.spy.net> <4sj76n$9fp@hptemp1.cc.umr.edu>
NNTP-Posting-Host: gold.acns.fsu.edu
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
In-Reply-To: <4sj76n$9fp@hptemp1.cc.umr.edu>
Xref: euryale.cc.adfa.oz.au comp.sys.sun.admin:70715 alt.sys.sun:10419 comp.unix.bsd.misc:1256 comp.unix.programmer:39707 comp.unix.bsd.bsdi.misc:4329 comp.unix.admin:45008


Thanks for all the help.  I recieved about 10 ping scripts that could do
the job.  I combined them and modified them into one gigantic ping script.
I have listed it at the end, it is very useful.  If any one has any
suggestions or modifications, please tell me.

here is what the ipnums file looks like:
40.33.1.1
168.223.20.130
128.186.2.206


here is what the ipdesc file looks like:

40.33.1.1 lilly.com Eli LIlly & Co.
168.223.20.130 cis.famu.edu  A FAMU computer
128.186.2.206 acns.fsu.edu A Florida State computer



Here is what the script looks like (I call it sonar):

#!/bin/sh

# by: Douglass Davis and others


# to run this script type: sh sonar & or ./sonar &
#
# This script will ping many computers
# and beep, send a page  and mail when
# a computer does not respond.
#
# a file called ipnums must be in the current
# directory.  It must contain the
# ip numbers of the hosts to ping
# 
# a file called ipdesc should be in the
# current directory with
# a list of ip numbers and descriptions.
# of the computers being  pinged.  The 
# numbers should correspond to those in the ipnums 
# file
#
# the local host's ip address should be
# first in the ip file to check local 
# connections first.
#
# this script will create a file called
# sonar.log in the current directory 


#names of important people to mail, separated by spaces.
MAIL_TO="yourname@youraddress.com ddavis@cis.famu.edu"

while [ /usr/bin/true ]
do

  for ip in `cat ipnums`
  do


    # do 3 pings.  Even if there is a good connection, 
    # there is a chance a ping may not respond. 

    ping $ip > /dev/null 2>&1
    rc1=$?
    ping $ip > /dev/null 2>&1
    rc2=$?
    ping $ip > /dev/null 2>&1
    rc3=$?
 
 
    # if no answer, then take action

    if [ $rc1 != 0  ] && [ $rc2 != 0 ] && [ $rc3 != 0 ]
    then
       
      # first get the description from the ipdesc file
      desc=`grep -w $ip ipdesc`
      if [ $? != 0 ] 
      then 
         desc=`echo $ip: no description available`
      fi
      desc=`echo \"$desc\"`

      # mail important people 
      echo "`date` PING TO $desc FROM `hostname` DID NOT RETURN" | fmt | mail -s "`hostname` communications error" $MAIL_TO 

      # do a display to the screen
      echo sonar: ping to $desc did not return 
      echo sonar: I will continue trying until I get an answer  
      echo 

      # add to log 
      echo `date`: PING TO $desc DID NOT RETURN | fmt >> sonar.log
      echo >> sonar.log


      # ping every 30 seconds, until we get an answer
      rc=-1
      while [ $rc != 0 ]
      do
        /usr/sbin/ping $ip > /dev/null 2>&1
        rc=$?
        sleep 30 
      done
 
      # The ping worked

      # mail important people 
      echo "`date` PING TO $desc  from `hostname` RETURNED communications recovered" | fmt | mail -s "`hostname` communications recovery" $MAIL_TO 

      # do a display to the screen 
      echo sonar: ping to $desc returned, communications recovered

      # add to log
      echo `date`: echo PING TO $desc RETURNED | fmt >> sonar.log
      echo >> sonar.log

    fi
  done
  
  #wait 5 Minutes, and do checks again
  sleep 300 
-one