linux shell自动侦察主机监控脚本

#!/bin/bash
#Check ESXI Server,If it's Power off,It can send mails to give an alarm.
#By Evans		Time:2019-12-16		Version:1.0
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#export LANG="zh_CN.UTF-8"  //如果发送邮件内容含有中文,取消这行注释
D_source=`find /* -name "Auto_Mail_Tools.sh"|sed "s/Auto_Mail_Tools.sh//g"`
list=$D_source/server.txt
date=`date +%Y-%m-%d@%H:%M:%S`
. /etc/init.d/functions
[ $UID != 0 ] && echo "[$date]Error:$0 is stop,Must be root." && exit 1  

function Check(){
     
[ ! -f $list ] && echo "[$date]Error:NO FOUND SERVER.LIST FILE IN $D_source." && exit 1
for IP in `cat $list|cut -f1 `
do
	ping $IP -c 1 -W2 > /dev/null
	if [ $? != 0 ];then
		action "[$date]Error:Ping $IP is Fail." /bin/false
		echo "*****[$IP Fail]*****"|mail -s "$IP WARNING $IP" 5966*****@qq.com
		[ $? != 0 ] && action "[$date]Sendmail is Fail." /bin/false
	else
		action "[$date]Ping $IP is success. " /bin/true
	fi
done
}

function main(){
     
cat << EOF
==========[Check_Server_List]=========
Time:$date
EOF
Check
exit 0
}

main
[ $? != 0 ] && echo -e "\E[1;31m This script run fail \E[0m" || echo -e "\E[1;32m This script run successful \E[0m"


注意:在脚本同目录下创建server.txt文件,将需要监控的主机ip写入其中即可

你可能感兴趣的:(shell脚本)