基于ICMP的网络监测报警代码

需要的库

	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()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值