python脚本

负责SVN,邮箱的帐号管理

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb
import paramiko
import urllib
import urllib2
from datetime import *
from optparse import OptionParser
import email
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import encoders
import smtplib
import mimetypes
grouplist=(‘all@spolo.org‘,
    ‘art@spolo.org‘,
    ‘gclient@spolo.org‘,
    ‘glueui@spolo.org‘,
    ‘glueapi@spolo.org‘,
    ‘glue@spolo.org‘,
    ‘gmeditor@spolo.org‘,
    ‘gluetil@spolo.org‘,
    ‘webapp@spolo.org‘)
def join_mail_group(conn, cur, mail_name, mail_group):
    if mail_group in grouplist:
        cur.execute(‘SELECT goto FROM vmail.alias where address="%s"‘ % mail_group)
        rows=cur.fetchall()[0][0]
        rows="\r\n".join([rows, mail_name])
        cur.execute(‘UPDATE vmail.alias set goto="%s" where address="%s"‘ % (rows,mail_group))
        conn.commit()
    else:
        print "please ensure mail_group correctly"
def deluser_mail_group(conn,cur,mail_name,mail_group):
    if mail_group in grouplist:
        cur.execute(‘SELECT goto FROM vmail.alias where address="%s"‘ % mail_group)
        rows=cur.fetchall()[0][0].replace("\r\n%s" % mail_name,"")
        cur.execute(‘UPDATE vmail.alias set goto="%s" where address="%s"‘ % (rows,mail_group))
        conn.commit()
    else:
        print "please ensure mail_group correctly"
def add_user(conn,cur, mail_name, user,maildir_stor):
    client=paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(‘192.168.1.4‘, 22, username=‘test‘, password="********", timeout=4)
    stdin,stdout,stderr = client.exec_command("openssl passwd -1 %s" % user)
    pwd=stdout.readlines()[0].replace("\n","")
    cur.execute(‘INSERT INTO vmail.mailbox(username,password,name,maildir,quota,domain,local_part)                  VALUES("%s","%s","%s","%s","1024","spolo.org","%s")‘ % (mail_name,pwd,user,maildir_stor,user))
    join_mail_group(conn,cur,mail_name,"all@spolo.org")
    print  "the user has been created"
def user_del(conn,cur, mail_name):
    cur.execute("DELETE FROM vmail.mailbox WHERE username=\"%s\"" % mail_name)
    deluser_mail_group(conn,cur,mail_name,"all@spolo.org")
    print "the user has been deleted!!!"
def svn_userdel(user,url):
    url_chanpin="http://192.168.4.10/admin/deluser.php"
    url_glue="http://glue.spolo.org/deluser.php"
    url_spp="http://spp.spolo.org/deluser.php"
    url_list=(‘url_chanpin‘,‘url_glue‘,‘url_spp‘)
    if url == ‘url_chanpin‘:
        http_oper(user,url_chanpin)
    elif url == ‘url_glue‘:
        http_oper(user,url_glue)
    elif url == ‘url_spp‘:
        http_oper(user,url_spp)
    else:
        print "the url is error ,plear rewrite!!!/n"
def http_oper(user,url):
    data={"username": user,"repeat_username": user,"operation":"1"}
    data=urllib.urlencode(data)
    req=urllib2.Request(url,
        data=data,
        headers={"Authorization": "Basic d2FuZ3poZW55dToxMjM2NTQ="})
    rep = urllib2.urlopen(req).read().decode(‘utf-8‘)
    print rep
def mail(user):
    from_addr = ‘postmaster@spolo.org‘
    to_addr = ‘%s@spolo.org‘ % user
    subject_header = ‘Subject: 员工手册‘
    attachment = u‘d:/员工手册.doc‘ #文件访问时候要求u,否则不能通过
    body = ‘‘‘欢迎加入*********科技发展有限公司,请查阅附件‘‘‘
    m = MIMEMultipart()
    m["To"] = "%s@spolo.org" % user
    m["From"] = "postmaster@spolo.org"
    m["Subject"] = subject_header
    ctype, encoding = mimetypes.guess_type(attachment)
    print ctype, encoding
    maintype, subtype = ctype.split(‘/‘, 1)
    print maintype, subtype
    m.attach(MIMEText(body, ‘plain‘, ‘utf-8‘))
    fp = open(attachment, ‘rb‘)
    msg = MIMEBase(maintype, subtype)
    msg.set_payload(fp.read())
    fp.close()
    encoders.encode_base64(msg)
    msg.add_header("Content-Disposition", "attachment", filename=‘员工手册.doc‘)
    m.attach(msg)
    smtp = smtplib.SMTP() 
    smtp.connect(‘mail.spolo.org:25‘) 
    smtp.login(‘postmaster@spolo.org‘, "123/bitlanyuan") 
    smtp.sendmail(‘postmaster@spolo.org‘, ‘%s@spolo.org‘ % user, m.as_string()) 
    smtp.quit()
    print ‘邮件发送成功‘
def main():
    usage="usage: %prog[-h][-a][-d]"
    parser=OptionParser(usage=usage)
    parser.add_option("-a",action="store_const", const=1, dest="add_user",help="add mailbox")
    parser.add_option("-d",action="store_const", const=1, dest="delete_user",help="delete mailbox")
    parser.add_option("-g",dest="user_group",help="join mail_group")
    parser.add_option("-G",dest="userdel_group",help="del user from group")
    parser.add_option("-n",dest="username",help="username")
    parser.add_option("-s",dest="svn",help="input owner‘s svn eg: url_chanpin,url_glue,url_spp")
    (options,args)=parser.parse_args()
    username=options.username
    url=options.svn
    mail_group=options.user_group
    conn=MySQLdb.connect(host=‘192.168.1.4‘,user=‘test‘,passwd="*********")
    cur=conn.cursor()
    dt=datetime.now()
    dt_format=dt.strftime(‘%Y.%m.%d.%H.%M.%S‘)
    userlist=username.split(‘,‘)
    if options.add_user:
        for user in userlist:
            maildir_stor=‘%s/%s/%s‘ % ("spolo.org", user, dt_format)
            mail_name=user+"@spolo.org"
            add_user(conn,cur, mail_name, user, maildir_stor)
            mail(user)
    if options.delete_user:
        for user in userlist:
            mail_name=user+"@spolo.org"
            user_del(conn, cur, mail_name)
    if options.user_group:
        join_mail_group(conn, cur, mail_name, mail_group)
    if options.userdel_group:
        deluser_mail_group(conn, cur, mail_name, mail_group)
    if options.svn:
        for user in userlist:
            svn_userdel(user, url)
    conn.close()
if __name__ == ‘__main__‘:
    main()


本文出自 “技术为王” 博客,谢绝转载!

python脚本,布布扣,bubuko.com

python脚本

上一篇:web8-文件包含


下一篇:Network - TCP实现聊天+文件上传