Question

I have a question about a script I've found to monitor a device on my network.

The script:

#!/bin/bash

HOSTS="192.168.11.1"
COUNT=1


SUBJECT="Ping failed"
EMAILID="me@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 "Host : $myHost is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID
fi
done

Upon executing the script, it seems to work, but it doesn't send anything to the email address. Could anyone tell me what I'm doing wrong?

Was it helpful?

Solution

Edit your script as follows and try again.

#!/bin/bash

HOSTS="192.168.11.1"
COUNT=1
SUBJECT="Ping failed"
EMAILID="me@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

cd ~

if [ -s PING_FILE ]
then
rm PING_FILE 
fi

echo "Host : $myHost is down (ping failed) at $(date)" > PING_FILE
mail -s "$SUBJECT" $EMAILID < PING_FILE

#if you want to get the file itself use the below code #
mutt -s "$SUBJECT" -a PING_FILE - $EMAILID < PING_FILE

fi

done

Regards,

Dominic

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top