阿铭每日一题 day 9 20180119

 

 

 

DAY9参考答案:ps~=不是唯一的答案

 1 #!/usr/bin/env python
 2 #-*- coding: UTF-8 -*-
 3 import os,sys
 4 reload(sys)
 5 sys.setdefaultencoding('utf8')
 6 import getopt
 7 import smtplib
 8 from email.MIMEText import MIMEText
 9 from email.MIMEMultipart import MIMEMultipart
10 from  subprocess import *
11 def sendqqmail(username,password,mailfrom,mailto,subject,content):
12     gserver = 'smtp.qq.com'
13     gport = 25
14     try:
15         msg = MIMEText(unicode(content).encode('utf-8'))
16         msg['from'] = mailfrom
17         msg['to'] = mailto
18         msg['Reply-To'] = mailfrom
19         msg['Subject'] = subject
20         smtp = smtplib.SMTP(gserver, gport)
21         smtp.set_debuglevel(0)
22         smtp.ehlo()
23         smtp.login(username,password)
24         smtp.sendmail(mailfrom, mailto, msg.as_string())
25         smtp.close()
26     except Exception,err:
27         print "Send mail failed. Error: %s" % err
28 def main():
29     to=sys.argv[1]
30     subject=sys.argv[2]
31     content=sys.argv[3]
32 ##定义QQ邮箱的账号和密码,你需要修改成你自己的账号和密码(请不要把真实的用户名和密码放到网上公开,否则你会死的很惨)
33     sendqqmail('1234567@qq.com','aaaaaaaaaa','1234567@qq.com',to,subject,content)
34 if __name__ == "__main__":
35     main()
36     
37     
38 #####脚本使用说明######
39 #1. 首先定义好脚本中的邮箱账号和密码
40 #2. 脚本执行命令为:python mail.py 目标邮箱 "邮件主题" "邮件内容"

 

 

 1 #!/bin/bash
 2 ip=123.23.11.21
 3 ma=abc@139.com
 4 while 1
 5 do
 6 ping -c10 $ip >/dev/null 2>/dev/null
 7 if [ $? != “0” ];then
 8 python /usr/local/sbin/mail.py $ma “$ip down” “$ip is down”
 9 #假设mail.py已经编写并设置好了
10 fi
11 sleep 30
12 done

 

posted @ 2018-01-21 10:21  Ivan_yyq  阅读(162)  评论(0编辑  收藏  举报