#!/usr/bin/env python2 #-*- coding:utf-8 -*- #Author : zhanglejie #Time: 2017/05/18 ‘‘‘ 生成wifi数据最近7天(不含当天)回传列表:天数,MAC 判断依据:数据回传后会创建相应的文件,文件名包含mac和时间(以天为分界), 获取文件名,提取出mac和时间,从而知道mac哪天有数据回传哪天没有 ‘‘‘ import os import datetime from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import smtplib #定义生成器,获取最近7天 def days7(): #获取当天时间 now = datetime.datetime.now() yield now - datetime.timedelta(1) yield now - datetime.timedelta(2) yield now - datetime.timedelta(3) yield now - datetime.timedelta(4) yield now - datetime.timedelta(5) yield now - datetime.timedelta(6) yield now - datetime.timedelta(7) #定义邮件发送 def sendmail(filename,email): #创建一个带附件的实例 msg = MIMEMultipart() #构造附件1 att1 = MIMEText(open(filename, ‘rb‘).read(), ‘base64‘, ‘utf8‘) att1["Content-Type"] = ‘application/octet-stream‘ att1["Content-Disposition"] = ‘attachment; filename=‘ + filename msg.attach(att1) #邮件内容 att2 = MIMEText("wifi数据回传检查\n",_subtype=‘plain‘,_charset=‘utf8‘) msg.attach(att2) #加邮件头 msg[‘to‘] = email msg[‘from‘] = ‘[email protected]‘ msg[‘subject‘] = ‘wifi data check‘ #发送邮件 try: server = smtplib.SMTP() server.connect(‘smtp.263.net‘) #XXX为用户名,XXXXX为密码 server.login(‘my email address‘,‘testtest‘) server.sendmail(msg[‘from‘], msg[‘to‘],msg.as_string()) server.quit() print ‘发送成功‘ except Exception,e: print str(e) #输入收件地址 email_name = raw_input("Please input your Email: ") print "######Running......" #获取文件名,并去重 con = set() for d in days7(): day = datetime.datetime.strftime(d,"%Y%m%d") #格式化时间,从时间格式转换成字符串 file_dir = os.popen("salt \"log*\" cmd.run ‘ls /home/survey_static/wifi/log/"+ day +"/* \‘|grep survey_static|awk -F \"/|_\" \‘$9 != \"\" {print $9\"_\"$7}\‘") for content in file_dir.readlines(): con.add(content.strip()) #拆分文件名,计算天数并保存到文件 con_list = list(con) mac_all = [] macs = set() ##获取mac,并去重 for i in con_list: mac_tmp = i.split("_")[0].replace(":","") mac_all.append(mac_tmp) macs.add(mac_tmp) ##计算天数,并保存到文件 file_name = "macs.csv" file_mac = open(file_name,"w") for m in list(macs): file_mac.write(str(mac_all.count(m)) + "," + m + "\n") file_mac.close() #发送邮件 sendmail(file_name,email_name) #原始日志举例: #/home/survey_static/wifi/log/20170504/data_78:a3:51:17:f5:bc_20170504.log #/home/survey_static/wifi/log/20170504/data_78:a3:51:15:38:c4_20170504.log #/home/survey_static/wifi/log/20170504/data_78:a3:51:17:f5:b8_20170504.log #处理后数据举例: # 7,78a3511802d8 # 7,78a35117ff18 # 7,78a3511802d4 # 7,78a35117fe84 # 2,78a3511802dc # 7,78a35117fe88 # 7,78a351180298 # 7,78a351180218 # 1,78a35117f810 # 7,78a35117f8e0
时间: 2024-10-10 18:29:21