Debian的网络监控脚本

时间:2014-05-08 19:04:58

标签: linux bash shell debian

我对我发现的用于监控网络设备的脚本有疑问。

剧本:

#!/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

执行脚本后,它似乎可以正常工作,但它不会向电子邮件地址发送任何内容。 谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

按如下方式编辑脚本,然后重试。

#!/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

此致