写在前面:
1. 初学python,使用python对vsftpd的虚拟用户进行增、删、查操作。
2. 小弟的英语比较菜,请忽略英语语法错误。^_^
3. 该程序主要练习MySQLdb、getpass模块的使用。需要安装MySQL-python软件包。
VSFTPD的使用,请参阅:http://blog.csdn.net/xrwwuming/article/details/51505857
代码
#!/usr/bin/env pyton
# -*- coding: utf-8 -*-
import MySQLdb
import getpass
import sys
mysqlHost = 'localhost'
mysqlUser = 'vsftp'
mysqlPass = 'ftpvuser'
mysqlDB = 'vsftp'
mysqlTable = 'vuser'
def connectDb():
global conn
global cursor
try:
conn = MySQLdb.connect(host=mysqlHost, user=mysqlUser, passwd=mysqlPass, db=mysqlDB)
cursor = conn.cursor()
connectResult = True
except:
print 'Error: Can not connect to mysql database, please check it, Quit.'
connectResult = False
return connectResult
def checkUser(comm):
cursor.execute(comm)
rows = cursor.rowcount
if rows != 0:
result = True
else:
result = False
return result
def closes():
cursor.close()
conn.close()
def tips():
raw_input('Enter any key to continue? ')
def userList():
comm = "select * from %s;" % mysqlTable
cursor.execute(comm)
rows = cursor.fetchall()
print 'Current Users'.center(40,'=')
for row in rows:
print 'Username: %s' % (row[0])
print
print 'Total User: %d' % (cursor.rowcount)
print 'END'.center(40,'=')
def add():
addUserResult = False
if connectDb():
while not addUserResult:
try:
username = raw_input(