首页 > 其他 > 详细

检查主机是否存活

时间:2014-03-27 19:10:58      阅读:415      评论:0      收藏:0      [点我收藏+]

1、检查主机是否存活:一旦主机ping不通,就发邮件。等到恢复就会再发次恢复邮件。


#!/bin/bash
# check server poweron or poweroff & mail
# 2014-03-21
set -u
[ -e "/shell/logfile" ] || mkdir -p /shell/logfile
ping -c 2 10.10.0.254 &> /dev/null
if [ $? -eq 0 ]; then
    if [ -e "/shell/logfile/poweroff.txt" ]; then
        echo "power on" | mail -s "power on" samzhou@test.com
        rm -f /shell/logfile/poweroff.txt
        touch /shell/logfile/poweron.txt
    else
        touch /shell/logfile/poweron.txt
    fi
elif [ ! $? -eq 0 ];then
    if [ -e "/shell/logfile/poweron.txt" ]; then
        echo "power off" | mail -s "power off" samzhou@test.com
        rm -f /shell/logfile/poweron.txt
        touch /shell/logfile/poweroff.txt
    else
        touch /shell/logfile/poweroff.txt
    fi
fi


2、检查多台主机是否存活

服务器列表serverlist.txt内容,servername:IP,一行一组。如:testserver:10.10.0.6。


#!/bin/bash
# check server ok
# 2014-02-26
set -u
check_fun() {
for host in $(cat /shell/serverlist.txt);do
{
servername=${host%:*}
serverip=${host#*:}
ping -c 2 $serverip &> /dev/null
if [ $? -eq 0 ]; then
    status=up
    printf "\033[1;32m%-4s \033[0m%-14s%-16s\n" $status $servername $serverip
else
    status=down
    printf "\033[1;31m%-4s \033[0m%-14s%-16s\n" $status $servername $serverip
fi
}&
done


检查主机是否存活,布布扣,bubuko.com

检查主机是否存活

原文:http://samlinux.blog.51cto.com/7988157/1385191

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!