標籤:
#!/usr/bin/python#coding=gb2312# auther = shaw # ver = 0.9import os,sys,smtplib,datetime,mimetypesimport email.mime.textfrom 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 = ‘[email protected]‘mail_password = ‘123456‘from_addr = mail_usernameto_addrs = [‘[email protected]‘,‘[email protected]‘]HOST = ‘smtp.sina.cn‘PORT = 25smtp = 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(‘[email protected]‘,‘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_addrmsg[‘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()
Python檢測日誌是否產生