代码中有Ansible的内容,需要搭建Ansible自动化工具,配置host文件和使用authorized_key模块来实现服务器互信,详情请见Ansible分类。
-
#! /usr/bin/env python
-
# -*- coding: utf-8 -*-
-
# auto. Mr.Wang
-
-
import smtplib
-
import os
-
import string
-
from email.mime.text import MIMEText
-
from random import choice
-
-
mailto_list=['XXXXXX@qq.com','XXXXXX@163.com'] #收件人(列表)
-
mail_host="smtp.163.com" #使用的邮箱的smtp服务器地址
-
mail_user="USER" #用户名
-
mail_pass="PASSWORD" #密码
-
mail_postfix="163.com" #邮箱的后缀
-
-
def GenPassword(length=20,chars=string.ascii_letters+string.digits):
-
return ''.join([choice(chars) for i in range(length)])
-
-
def send_mail(to_list,sub,content):
-
me="Mr.Wang"+"<"+mail_user+"@"+mail_postfix+">"
-
msg = MIMEText(content,_subtype='plain')
-
msg['Subject'] = sub
-
msg['From'] = me
-
msg['To'] = ";".join(to_list) #将收件人列表以‘;’分隔
-
try:
-
server = smtplib.SMTP()
-
server.connect(mail_host) #连接服务器
-
server.login(mail_user,mail_pass) #登录操作
-
server.sendmail(me, to_list, msg.as_string())
-
server.close()
-
return True
-
except Exception, e:
-
print str(e)
-
return False
-
-
pasd = GenPassword()
-
os.system("ansible Test -m shell -a 'echo \"%s\" | passwd --stdin root'"%(pasd))
-
if send_mail(mailto_list,"WFJ Server Change Password",pasd): #邮件主题和邮件内容
-
print "发送成功!"
-
else:
-
print "发送失败!"
阅读(2297) | 评论(0) | 转发(0) |