巴什ping状态脚本 [英] Bash ping status script

查看:142
本文介绍了巴什ping状态脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了下面的脚本

HOSTS="ns1.server.com ns2.server.com"
SUBJECT="Host Down"

for myHost in $HOSTS
do
count=$(ping -c 10 $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{               
print $1 }')
if [ $count -eq 0 ]; then
echo "Host : $myHost is down (ping failed) at $(date)" | sendEmail -f email (email address removed) -u "$SUBJECT" etc etc
fi
done

通过cron运行每隔5分钟然而,当一台主机宕机,我会接受并每5分钟反映此电子邮件。我想是这样,它只是我的电子邮件时,状态已更改为添加的功能。也就是说,如果它下来,我不希望它发送任何进一步的更新,直到它的了。

Run via cron every 5 minutes however when a host is down I will receive and email every 5 minutes reflecting this. What i'd like is to add the function so that it only emails me when the status has changed. ie if it's down I don't want it to send any further updates until it's up.

推荐答案

我觉得这样的事情可以帮助:

I think something like this can help:

#!/bin/bash

HOSTS="ns1.server.com ns2.server.com"
HOSTS="123.123.1.1 ns1.server.com"
SUBJECT="Host Down"

ping_attempts=1
down_hosts=down_hosts.txt

for myHost in $HOSTS
do
        count=$(ping -c $ping_attempts $myHost | awk -F, '/received/{print $2*1}')
        echo $count
        if [ $count -eq 0 ]; then
                echo "$myHost is down"
                if  [ $(grep -c "$myHost" "$down_hosts") -eq 0 ]; then
                        echo "Host : $myHost is down (ping failed) at $(date)"
                        echo "$myHost" >> $down_hosts
                fi
        else
                echo "$myHost is alive"
                if  [ $(grep -c "$myHost" "$down_hosts") -eq 1 ]; then
                        echo "Host : $myHost is up (ping ok) at $(date)"
                        sed -i "/$myHost/d" "$down_hosts"
                fi
        fi
done

这篇关于巴什ping状态脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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