标签:
#!/usr/bin/python #coding=gb2312 # auther = shaw # ver = 0.9 import os,sys,smtplib,datetime,mimetypes import email.mime.text from email.Header import Header ############################################## os.system(‘hostname > /home/ztgame/hostname.txt‘) f = file(‘/home/ztgame/hostname.txt‘) for i in f.readlines(): hostname = i.strip(‘\n‘) f.close() os.remove(‘/home/ztgame/hostname.txt‘) day = datetime.date.today() - datetime.timedelta(days=1) B = [‘***‘, ‘Backup‘, ‘success!‘] log = [] f = file(‘/tmp/log.txt‘,‘r+‘) for i in f.readlines(): A = i.strip(‘\n‘).split() log.append(A) f.close() if B == log[-1]: print ‘*** \033[32;2m/tmp/log.txt Backup Success\033[0m.‘ sys.exit(0) else: mail_username = ‘admin@sina.cn‘ mail_password = ‘123456‘ from_addr = mail_username to_addrs = [‘admin@ztgame.com‘,‘admin@sina.com‘] HOST = ‘smtp.sina.cn‘ PORT = 25 smtp = smtplib.SMTP() print ‘connecting ...‘ #smtp.set_debuglevel(1) try: print smtp.connect(HOST,PORT) except: print ‘\033[31;2mCONNECT ERROR \033[0m****‘ smtp.starttls() try: print ‘loginning ...‘ smtp.login(‘xtops@sina.cn‘,‘Ahxhit12#$‘) except: print ‘LOGIN ERROR ****‘ msg = email.mime.text.MIMEText("""Dear shaw: 警告:%s %s的/tmp/log.txt备份发生错误。 """ % (hostname,day),_subtype=‘plain‘, _charset=‘gb2312‘) msg[‘From‘] = from_addr msg[‘To‘] = ‘;‘.join(to_addrs) msg[‘Subject‘]=Header(‘log.txt日志备份出错‘, charset=‘gb2312‘) print msg.as_string() smtp.sendmail(from_addr,to_addrs,msg.as_string()) smtp.quit()
标签:
原文地址:http://my.oschina.net/u/2428313/blog/486987