#coding: UTF-8
import smtplib
import time,sys,requests
from time import sleep
from subprocess import run,PIPE
from email.mime.text import MIMEText
from email.header import Header
#获取时间
ipaddress=input("请输入需要检测ip地址\n")
hostname=input('请输入该IP标识名,例如:***\n')
# 第三方 SMTP 服务
mail_host = "smtp.qq.com" # 设置服务器
mail_user = "***********@qq.com" # 用户名
mail_pass = "***************" # 口令
sender = '*************@qq.com'
receivers = ['**********@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
count=1
sleeptime=0
def sendmail():#发送邮件
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
while True:
localtime = time.asctime(time.localtime(time.time()))#格式化当前时间
message = MIMEText('IP地址:' + ipaddress + '断网了!' + '断网时间' + localtime, 'plain', 'utf-8')
message['From'] = Header("*****", 'utf-8')#这里指发送者名字
message['To'] = Header("******", 'utf-8')#接收者名字
subject = '淡定点,可能是掉包'
if count>4:
subject='他妈的网炸了'
message['Subject'] = Header(hostname + subject + ',测试IP地址:' + ipaddress + ',断网时间' + localtime, 'utf-8')
r = run('ping '+ipaddress,stdout=PIPE,stderr=PIPE,stdin=PIPE,shell=True)
if r.returncode:
print('IP'+ipaddress+'断网发生第{}次'.format(count)+'发生时间:'+localtime)
count+=1
if count ==5:
sleeptime=count*60#4次之后防骚扰持续发送邮件
try:
sendmail()
except smtplib.SMTPException:
print("Error: 无法发送邮件")
else:
print(hostname+'网炸了吗?没炸')
sleeptime=30
print('冷却邮件发送'+str(sleeptime)+'秒')
sleep(sleeptime)
检测服务器状态出现问题发送邮件
最新推荐文章于 2023-04-20 17:53:13 发布