Chinaunix首页 | 论坛 | 博客
  • 博客访问: 236874
  • 博文数量: 52
  • 博客积分: 1492
  • 博客等级: 上尉
  • 技术积分: 554
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-09 15:54
文章分类
文章存档

2010年(52)

我的朋友

分类:

2010-06-25 14:41:05

    之前有写了一个自动登录各个服务器的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'
阅读(522) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~