ping script

hello fellows,
I need help with a script, I'm using this one

HOSTS="192.168.10.9 192.168.10.15 "  

SUBJECT="Attention - Gateway San Martin DOWN "
COUNT=1

EMAILID="lmail@mydomain.com"
for myHost in $HOSTS
do
 	 count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
 	 if [ $count -eq 0 ]; then
    
  	  echo "the host $myHost is DOWN. Date:  $(date)" | mail -s "$SUBJECT" $EMAILID

fi
done

now I need this just to send me one mail, and send me another one when the host y back up. I have no idea how to do that.

can you help me?

thanks,
lucas

PS: excuse me for my english, it's no my native language

One way of doing this is to use an infinite while loop and put the script in sleep periodically:-

while (true)
do

HOSTS="192.168.10.9 192.168.10.15 "  
SUBJECT_D="Attention - Gateway San Martin DOWN "
SUBJECT_U="Gateway San Martin is UP"
COUNT=1
SLEEP_INTERVAL="300"
EMAILID="lmail@mydomain.com"

for myHost in $HOSTS
do
      count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
      if [ $count -eq 0 ]; then
         echo "the host $myHost is DOWN. Date:  $(date)" | mail -s "$SUBJECT_D" $EMAILID
      else
         echo "the host $myHost is UP. Date:  $(date)" | mail -s "$SUBJECT_U" $EMAILID
         exit 0
      fi
done

sleep ${SLEEP_INTERVAL}

done

Or you can schedule this script in cron without infinite while loop & sleep.

hi, thanks a lot, but it looks like the same, if the host is up it sends me an email every 300 seconds, same as if it's down.
I need the script to send me one email when it goes down, and one when it is back up.
con you help me?
thanks in advance