需要的库
itchat
stmp
##code
#coding=utf-8
__author__ = '尤也夫'
__version__ = '1.0.2'
__datetime__ = '2018-02-08'
import sys
import os
import argparse
import time
import subprocess
import smtplib
import cmd
import itchat
import re
from email.mime.text import MIMEText
mailto_list=['xx@xx.com'] #收件人(列表)
mail_host="" #使用的邮箱的smtp服务器地址
mail_user="" #用户名
mail_pass="" #密码
mail_postfix="" #邮箱的后缀
mail_content=""
itchat.auto_login(hotReload=True)
def send_mail(to_list,sub,content):
me="hello"+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content,_subtype='plain')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list) #将收件人列表以‘;’分隔
try:
server = smtplib.SMTP_SSL("smtp.qq.com",465)
server.connect(mail_host) #连接服务器
server.login(mail_user,mail_pass) #登录操作
server.sendmail(me, to_list, msg.as_string())
server.close()
return True
except Exception,e:
return False
for i in range(1): #发送1封,上面的列表是几个人,这个就填几
if send_mail(mailto_list,sub,content):
print "done"
else:
print "failed"
def subping():
f = open("ip_list.txt","r") #根目录下新建ip_list.txt,填写需要检测的ip地址
lines = f.readlines()
for line in lines:
line=line.strip('\n')
ret = subprocess.call("ping -c 1 -w 10 %s" % line,shell=True,stdout=subprocess.PIPE)
time.sleep(1)
if ret == 0:
str = '%s is alive ' % line
else:
str = '%s 搞事情了,大新闻,我要查一查,是要为结果负责任滴' % line #放宽判断重复检测
print str,
sj = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
print sj
time.sleep(5)
wtf_check= subprocess.call("ping -c 5 -w 50 %s" % line,shell=True,stdout=subprocess.PIPE)
t = int(time.strftime('%H',time.localtime(time.time())))
if ret == 0:
str = '%s 虚惊一场' % line
continue
else:
str ='%s' % line
print str
if str == '192.168.100.1':
str=''
elif str =='192.168.100.2':
str=''
elif str =='192.168.100.3':
str='' #根据ip更改通知字符串
send_mail(mailto_list,str,mail_content)
users = itchat.search_friends(name='') #发送给谁
userName = users[0]['UserName']
itchat.send(str,toUserName = userName)
deline=line
f.close()
du = open("ip_list.txt","r")
dulines = du.readlines()
xie = open("ip_list.txt.new","w")
for xieline in dulines: #排除不在线ip地址
if deline in xieline:
continue
xie.write(xieline)
du.close()
xie.close()
os.remove("ip_list.txt")
os.rename("ip_list.txt.new","ip_list.txt")
time.sleep(2)
continue
f.close()
var = 1
while(var==1):
subping()