Python创建、删除、查询vsftpd的虚拟用户(虚拟用户使用MySQL数据库)

这篇博客介绍了如何使用Python对VSFTPD的虚拟用户进行管理,包括通过MySQL数据库进行增删查操作。文章重点练习了MySQLdb和getpass模块的应用,同时也提醒读者需先安装MySQL-python软件包。
摘要由CSDN通过智能技术生成

写在前面:
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(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值