之前有写了一个自动登录各个服务器的python脚本,下面这个脚本可以配合那个脚本使用,用自动登录的脚本先取到各个服务器的信息,然后汇总到server_msg.txt中,再通过下面的邮件脚本发送的管理员的邮箱。。。我把它用到服务器上,是可行的。服务器OS rhel5.1
#!/usr/bin/env python2.4
# filename:email_sta.py
#coding=utf-8
import smtplib, mimetypes
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEImage import MIMEImage
msg = MIMEMultipart()
msg['From'] = "severs"
msg['To'] = 'linuxsa'
msg['Subject'] = 'servers_status'
#添加邮件内容
txt = MIMEText("company server information")
msg.attach(txt)
#添加二进制附件
fileName = r'/App/alert/SH/server_msg.txt'
ctype, encoding = mimetypes.guess_type(fileName)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
att1 = MIMEImage((lambda f: (f.read(), f.close()))(open(fileName, 'rb'))[0], _subtype = subtype)
att1.add_header('Content-Disposition', 'attachment', filename = fileName)
msg.attach(att1)
#发送邮件
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com:25')
smtp.login('xxxx', '*****')
smtp.sendmail(, , msg.as_string())
smtp.quit()
print 'Mail sent successfully'