脚本电子邮件警报和ping [英] Script for email alert and ping

查看:363
本文介绍了脚本电子邮件警报和ping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助来更新这个脚本,如果ping失败,将再派平到另一台主机(除了现在发送,如果ping失败的电子邮件)。
这怎么可能从这个脚本呢?

 #!/斌/庆典HOSTS =IP地址
COUNT = 4对HOSTS $ MYHOST

    数= $(平-c $ COUNT $ MYHOST | grep'可以接受'| awk的-F',''{$打印2}| $一个
    如果[$算上-eq 0];然后
        #100%不合格
        回声服务器无法在$(日期)|邮件-s服务器关闭myadress@gmail.com
        回声主持人:$ MYHOST下跌(平失败)在$(日期)
    科幻
DONE


解决方案

您可以把的东西,在一个函数。你并不需要处理(的grep )的结果是:你可以依靠返回退出状态。

 #!/斌/庆典
HOSTS =IP1 IP2 IP3 IP4 IP5
COUNT = 4pingtest(){
  在$ @MYHOST
  做
    平-c$ COUNT$ MYHOST&放大器;&安培;返回1
  DONE
  返回0
}如果pingtest $ HOSTS
然后
  #100%不合格
  回声服务器无法在$(日期)|邮件-s服务器关闭myadress@gmail.com
  回声所有主机($主机)下降(平失败)在$(日期)
科幻

I need help to update this script to if ping fails it would send another ping to another host (besides the email which is sent now if ping fails). How can this be done from this script?

#!/bin/bash

HOSTS="IP ADRESS"
COUNT=4

for myHost in $HOSTS
do
    count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | a$
    if [ $count -eq 0 ]; then
        # 100% failed
        echo "Server failed at $(date)" | mail -s "Server Down" myadress@gmail.com
        echo "Host : $myHost is down (ping failed) at $(date)"
    fi
done

解决方案

You can put the ping stuff in a function. You do not need to process (grep) the ping result: you can rely on ping return exit status.

#!/bin/bash
HOSTS="IP1 IP2 IP3 IP4 IP5"
COUNT=4

pingtest(){
  for myHost in "$@"
  do
    ping -c "$COUNT" "$myHost" && return 1
  done
  return 0
}

if pingtest $HOSTS
then
  # 100% failed
  echo "Server failed at $(date)" | mail -s "Server Down" myadress@gmail.com
  echo "All hosts ($HOSTS) are down (ping failed) at $(date)"
fi

这篇关于脚本电子邮件警报和ping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆