[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 ''' 次脚本为自动创建ftp用户和密码,减少运维对ftp的操作,给xx部门使用。 当ftp根目录下有新的目录时,自动把目录转换成账户和密码 pinyin模块把汉字转换成拼音,用于ftp用户和密码 使用方式: 1:初次使用可以先建个管理员帐号,指到ftp根目录 2:使用管理员帐号在根目录加目录即可,然后此脚本自动创建用户指到该目录 ''' 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(" ") ret.append("新文件夹") 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(" ") 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+" ") pam_file.close() f_passwd = open('vuser_passwd','a') f_passwd.write(C_to_E + " " + C_to_E+"123" + " ") f_passwd.flush() f_passwd.close os.system('db_load -T -t hash -f vuser_passwd vuser_passwd.db') print"成功创建ftp账户%s和认证文件"%(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)