python操作数据库和urllib及paramiko的使用

#!/usr/bin/env python

import MySQLdb

import paramiko

import urllib

import urllib2

from datetime import *

from optparse import OptionParser

grouplist=('[email protected]',

'[email protected]',

'[email protected]',

'[email protected]',

'[email protected]',

'[email protected]',

'[email protected]',

'[email protected]',

'[email protected]')

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" % rows where address="%s" % 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(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='root', 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))

print  "the user has been created"

def user_del(cur, mail_name):

cur.execute("DELETE FROM vmail.mailbox WHERE username=\"%s\"" % mail_name)

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 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='root',passwd="**********")

cur=conn.cursor()

dt=datetime.now()

dt_format=dt.strftime('%Y.%m.%d.%H.%M.%S')

userlist=username.split(',')

#maildir_stor='%s/%s/%s' % ("spolo.org", username, dt_format)

#mail_name=options.username+"@spolo.org"

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(cur, mail_name, user, maildir_stor)

if options.delete_user:

for user in userlist:

mail_name=user+"@spolo.org"

user_del(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)