Python监控主机是否存活并以邮件报警

Python监控主机是否存活并以邮件报警,第1张

概述利用Python写了简单测试主机是否存活脚本,此脚本不适于线上使用,因为网络延迟、丢包现象会造成误报邮件,那么后续会更新判断三次ping不通后再发报警邮件,并启用多线程处理。

利用Python写了简单测试主机是否存活脚本,此脚本不适于线上使用,因为网络延迟、丢包现象会造成误报邮件,那么后续会更新判断三次Ping不通后再发报警邮件,并启用多线程处理。

#!/usr/bin/env python # Coding:UTF-8 import time import pexpect import smtplib from email.mime.text import MIMEText mail_host = "smtp.163.com"    #定义smtp服务器 mail_to = "[email protected]" #邮件收件人 mail_from = "[email protected]"   #邮件发件人 mail_pass = "123456"      #邮件发件人邮箱密码 while True:   def Mail(error_ip):     date = time.strftime('%Y-%m-%d %H:%M:%s')     msg = MIMEText("%s Ping %s Failed from 255.252." % (date,error_ip))     msg['Subject'] = "Ping %s Failed." % error_ip  #定义邮件主题     msg['From'] = mail_from     msg['To'] = mail_to     try:       s = smtplib.SMTP()        #创建一个SMTP()对象       s.connect(mail_host,"25")      #通过connect方法连接smtp主机       s.starttls()          #启动安全传输模式       s.login(mail_from,mail_pass)     #邮箱账户登录认证       s.sendmail(mail_from,mail_to,msg.as_string()) #邮件发送       s.quit()   #断开smtp连接     except Exception,e:       print str(e)   ip_List = ['192.168.18.10','192.168.18.11','192.168.18.12']   for ip in ip_List:     Ping = pexpect.spawn('Ping -c 1 %s' % ip)     check = Ping.expect([pexpect.TIMEOUT,"1 packets transmitted,1 received,0% packet loss"],2)  #2代表超时时间     if check == 0:       Mail(ip)       print "Ping %s Failed,Have email." % ip     if check == 1:       print "Ping %s successful." % ip   print "Sleep 10s..."  time.sleep(10)#直接运行# python Ping.py Ping 192.168.18.10 successful.Ping 192.168.18.11 successful.Ping 192.168.18.12 successful.Sleep 10s...

以上就是本文的全部内容,希望对大家学习Python监控主机是否存活并以邮件报警有所帮助。

总结

以上是内存溢出为你收集整理的Python监控主机是否存活并以邮件报警全部内容,希望文章能够帮你解决Python监控主机是否存活并以邮件报警所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1204906.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存