Bash |主机检查

时间:2016-02-16 08:48:08

标签: bash

我的bash脚本有点问题 我有一个学校项目,我必须制作一个bash脚本来检查主机是否每5分钟一次,如果失败发送电子邮件

我遇到了" fi"声明但修正了错误。 现在,当我运行脚本时,我得到一个错误:第17行到很多参数" 它启动ping命令(我的Anti病毒阻止了ICMP,所以我知道ping行有效)

#!/bin/bash

#Server-status script

FAILS=0
EMAIL_ADDRESS="Critical-error@gruppe4.net" ##Email capabilities 
SERVER="192.168.1.1"  ###Host to check
SLEEP=300           ###Defining Sleep

while true; do
ping -c 1 $SERVER >/dev/null 2>&1
if [ $? -ne 0 ] ; then #if ping exits nonzero...
    FAILS=$"[FAILS + 1]"
else
    FAILS=0
fi
if [ $FAILS -gt 4 ]; then
    FAILS=0
    echo "Server $SERVER is offline!" \
        | mail -s "Server offline" "$EMAIL_ADDRESS"
fi
sleep $SLEEP #check again in SLEEP seconds
done

2 个答案:

答案 0 :(得分:0)

使用declare -i将FAILS用作整数并初始化为0

declare -i FAILS=0

然后加1

FAILS=$FAILS+1 

这是我的代码(我已经评论了邮件命令):

#!/bin/bash

#Server-status script

declare -i FAILS=0
EMAIL_ADDRESS="Critical-error@gruppe4.net" ##Email capabilities 
SERVER="192.168.1.1"  ###Host to check
SLEEP=1           ###Defining Sleep

echo "1-FAILS[$FAILS]"

while true; do
   ping -c 1 $SERVER >/dev/null 2>&1
   if [ $? -ne 0 ] ; then #if ping exits nonzero...
       FAILS=$FAILS+1 
   else
       FAILS=0
   fi

   echo "2-FAILS[$FAILS]"

   if [ $FAILS -gt 1 ]; then
       FAILS=0
       echo "Server $SERVER is offline!" # \ | mail -s "Server offline"   "$EMAIL_ADDRESS"
   fi
       sleep $SLEEP #check again in SLEEP seconds
done

输出:

sh-4.3$ bash -f main.sh                                                                                                                                                                                                                                
  1-FAILS[0]                                                                                                                                                                                                                                             
  2-FAILS[1]                                                                                                                                                                                                                                             
  2-FAILS[2]                                                                                                                                                                                                                                             
  Server 192.168.1.1 is offline!                                                                                                                                                                                                                         
  2-FAILS[1]                                                                                                                                                                                                                                             
  2-FAILS[2]                                                                                                                                                                                                                                             
  Server 192.168.1.1 is offline!                                                                                                                                                                                                                         
  2-FAILS[1]                                                                                                                                                                                                                                             
  2-FAILS[2]                                                                                                                                                                                                                                             
  Server 192.168.1.1 is offline!                                                                                                                                                                                                                         
  2-FAILS[1]  

我希望这可以提供帮助

此致 克劳迪奥

答案 1 :(得分:0)

您正在使用内容$FAILS扩展[FAILS + 1]变量,并获得无效的[]语法。

FAILS=$"[FAILS + 1]"更改为:

FAILS=$((FAILS+1))