命令完毕自动发送邮件提醒执行结果

来源:互联网 发布:淘宝介入卖家给假地址 编辑:程序博客网 时间:2024/05/16 10:17

经常碰到一个命令

#!/usr/bin/python# -*- encoding:utf-8 -*-import sys import commandsfrom email.mime.text import MIMEText import smtplibclass sendMail():    def txtMail(self,content):        msg = MIMEText(content,_subtype='plain',_charset='gb2312')        msg['to'] = '18351003638@139.com'        msg['from'] = 'exec@9ishell.com'        msg['subject'] = 'exec result'        try:            server = smtplib.SMTP()            server.connect('smtp.mxhichina.com')            server.login('exec@9ishell.com','password')            server.sendmail(msg['from'], msg['to'],msg.as_string())            server.quit()            print("发送成功")        except Exception as e:            print(str(e))if __name__=='__main__':    comm= ' '.join( [ str(x) for x in sys.argv[1:]])    (status, output) = commands.getstatusoutput(comm)    sendMail().txtMail("comm:%s;status:%s;output:%s" % (comm,status,output))
0 0