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?

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top