문제

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