Ping – Host Check

A short script to perform a continuous ping to check the availability of a certain server / host.

execute:
./pingscript.sh <ping count> <target> <additional delay>
./pingscript 5 192.168.0.1 25

 

#!/bin/bash

# set varables
COUNT=$1
TARGET=$2
DELAY=$3
COUNT=0

LOGFILE="./host_check.log"


# if delay is empty use default 10
if [ "$DELAY" == "" ];
 then
   DELAY="10"
fi

# infinite loop
while [ $COUNT -lt 10 ];
 do

# create date
DATE=$(date "+%Y%m%d-%H%M%S")
COMMAND=$(ping -c 10 $TARGET  | grep "packet loss" | awk '{ print $6 }' | sed -e 's/%//g')

# write on console
echo $DATE"-"$TARGET"-LOSS-"$COMMAND

# if host is not reachable or any ping is lost 
# your result will be logged
if [ "$COMMAND" != "0" ];
 then
        echo $DATE"-"$TARGET"-LOSS-"$COMMAND >> $LOGFILE
fi

sleep $DELAY

done

Output:

#./pingcheck.sh 5 192.168.0.1 5
20110724-144555-192.168.0.1-LOSS-0%
20110724-144609-192.168.0.1-LOSS-0%

1 comment for “Ping – Host Check

  1. feet fetish chat
    April 26, 2014 at 15:39

    Hi there to every one, the contents present at this web page are
    in fact awesome for people experience, well, keep up the
    good work fellows.

Leave a Reply

Your email address will not be published. Required fields are marked *