python爬蟲抓取zabbix監控圖,並發郵件
阿新 • • 發佈:2017-10-21
python 抓取
最近十九大非常煩,作為政府網站維護人員,簡直是夜不能寐。各種局子看著你,內保局,公安部,360,天融信,華勝天成,中央工委,政治委員會...
360人員很傻X,作為安全公司,竟然不能抓到XX網站流量,在我們機房放的探針更是搞笑,讓我們手工上報流量數據。白天還行,晚上怎麽辦?給他寫個腳本吧
首先,用requests抓取zabbix流量圖
#-*- coding:utf-8 -*- import requests,os import datetime chrome=‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36‘ header = {‘User-Agent‘:chrome} os.chdir(‘C:\\Users\\Administrator\\Desktop‘) s=requests.session() loginurl=‘http://10.1.2.61/index.php‘<br>#如果是中文,enter裏是 註冊。如果是英文,enter裏是sign in logininfo = {"name":‘zabbix的用戶名‘,‘password‘:‘zabbix密碼‘,‘autologin‘:1,"enter":‘註冊‘} req=s.post(loginurl,data=logininfo,headers=header) now=datetime.datetime.now() nowtime=now.strftime(‘%Y%m%d%H%M%S‘) durl=‘http://10.1.2.61/chart2.php‘ payload={‘graphid‘:715, ‘period‘:3600, ‘stime‘:nowtime} picurl=s.get(durl,params=payload,headers=header) with open (‘1.png‘,‘wb‘) as code: code.write(picurl.content)
接著發帶圖片的郵件,下面這個腳本從網上抄的。偷個懶
#-*- coding:utf-8 -*- import smtplib,os from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.header import Header from email.utils import parseaddr, formataddr import zabbix # 格式化郵件地址 def formatAddr(s): name, addr = parseaddr(s) return formataddr((Header(name, ‘utf-8‘).encode(), addr)) def sendMail(body, image): smtp_server = ‘郵箱smtp地址‘ from_mail = ‘郵箱帳號‘ mail_pass = ‘郵箱密碼‘ to_mail = [‘收件人1‘,‘收件人2‘,‘收件人3‘,‘收件人4‘,‘收件人5‘,‘收件人6‘] msg = MIMEMultipart() msg[‘From‘] = formatAddr(‘紫光閣監控 <%s>‘ % from_mail).encode() msg[‘To‘] = ‘,‘.join(to_mail) msg[‘Subject‘] = Header(‘監控‘, ‘utf-8‘).encode() msg.attach(MIMEText(body, ‘html‘, ‘utf-8‘)) with open(image, ‘rb‘) as f: msgImage = MIMEImage(f.read()) msgImage.add_header(‘Content-ID‘, ‘<image1>‘) msg.attach(msgImage) try: s = smtplib.SMTP() s.connect(smtp_server, "25") s.login(from_mail, mail_pass) s.sendmail(from_mail, to_mail, msg.as_string()) s.quit() except smtplib.SMTPException as e: print (‘發送失敗‘) if __name__ == "__main__": # os.chdir(‘C:\\Users\\Administrator\\Desktop‘) body = """ <h1>紫光閣監控圖片</h1> <img src="cid:image1"/> """ sendMail(body, ‘1.png‘) os.remove(‘1.png‘)
python爬蟲抓取zabbix監控圖,並發郵件