vsftp manage users

[root@localhost vsftpd]# cat auto_createftp.py 
#!/usr/bin/env python
#_*_coding:utf-8_*_
#date:20180502
#author:lihongxing



import time,os,sys
from xpinyin import Pinyin
#import pypinyin
#from pypinyin import pinyin,lazy_pinyin

'''
The second script is to automatically create the ftp user and password, reduce the operation and maintenance of the ftp operation, and use it for the xx department.
When there is a new directory under the ftp root directory, the directory is automatically converted into an account and password
The pinyin module converts Chinese characters into pinyin for ftp users and passwords
How to use:
1: For the first time, you can create an administrator account and point to the ftp root directory
2: Use the administrator account to add a directory to the root directory, and then this script automatically creates a user to point to the directory
'''
if sys.getdefaultencoding() != 'utf-8':
    reload(sys)
    sys.setdefaultencoding('utf-8')

p = Pinyin()


dir_path_info = ' ls / DATA / rocen-ftp / ' 
dir_ret = os.popen (dir_path_info)
ls_ret = dir_ret.read ()
ret = ls_ret.strip(" ").split("\n")
ret.append( " New folder " )

while True:
    new_dir_path_info = 'ls /DATA/rocen-ftp/'
    new_dir_ret = os.popen(new_dir_path_info)
    new_ls_ret = new_dir_ret.read ()
    new_ret = new_ls_ret.strip(" ").split("\n")
    different_list = list(set(new_ret).difference(set(ret)))
    #print "ret:",ret,"new_ret:",new_ret
    if len(different_list) >0:
        ulist = []
        for i in different_list:
            dir_name_path = "/DATA/rocen-ftp/%s"%(i)
            if os.path.isdir(dir_name_path):
                l =i.decode("utf-8")
                ulist.append(l)
            ret.append(i)
        for dir_name in ulist:
            C_to_E = p.get_pinyin(dir_name,splitter='')
            #print C_to_E

            pam_path = './vuser_conf/' + C_to_E
            pam_file = open(pam_path,'w+')
            pam_text_info = '''local_root=%s
write_enable=YES
anon_umask=022
anon_world_readable_only=NO
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES''' %(dir_name_path)
            pam_file.write(pam_text_info+"\n")
            pam_file.close()

            f_passwd = open('vuser_passwd','a')
            f_passwd.write(C_to_E + "\n" + C_to_E+"123" + "\n")
            f_passwd.flush()
            f_passwd.close
            os.system( ' db_load -T -t hash -f vuser_passwd vuser_passwd.db ' )
             print " successfully created ftp account %s and authentication file " % (C_to_E)
            os.system("chmod 777 -R %s" %(dir_name_path))
            os.system("service vsftpd restart")
            os.system('db_load -T -t hash -f vuser_passwd  vuser_passwd.db')
            os.system("service vsftpd restart")
    time.sleep(5)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325348703&siteId=291194637