#!/bin/sh

#
# help() To print help
#
help()
{
  echo "Usage: $0 -h <host> -s command -f command "
  echo "Options: The following are valid arguments"
  echo " -s command to run when ping is successfull"
  echo " -f command to run when ping fails"
  echo " -h host or IP to ping"
  echo "NOTE: commands included with an argument should be enclosed in quotes for proper results"
  exit 1
}

#
#if no argument
#
if [ $# -lt 1 ]; then
  help
fi

while getopts s:f:h: opt
do
        case "$opt" in
                s) success="$OPTARG";;
                f) fail="$OPTARG";;
                h) host="$OPTARG";;
                \?) help;;
        esac
done

if [ "$host" == "" ] || [ "$success" == "" ] || [ "$fail" == "" ] ; then
	echo "$host"
	echo "$success"
	echo "$fail"
	help
fi

if [ "`ping -c 5 $host | grep received | cut -d, -f2`" == " 0 received" ] ; then
	$fail
else
	$success
fi
