python 自动创建vsftp

  1 [root@js-93 vsftp_install]# cat install_vsftp.py 
  2 #!/usr/bin/env python3
  3 #coding:utf-8
  4 #install vsftp service
  5 
  6 import os,sys,subprocess
  7 import getpass
  8 def install_vsftp():
  9     check_open = os.system('rpm -qa|grep openssh-server >/dev/null 2>&1')
 10     check_vsftp = os.system('rpm -qa|grep vsftp >/dev/null 2>&1')
 11     if check_open != 0 :
 12         check_open_second = os.system('yum install openssh-server -y >/dev/null 2>&1')
 13         if check_open_second != 0:
 14             print('Error: install openssh-server faild ,please check your system')
 15         else:
 16             print('openssh-server install successful')
 17     else:
 18         print('openssh-server already installed')
 19     if check_vsftp != 0:
 20         check_vsftp_second = os.system('yum install vsftpd -y >/dev/null 2>&1')
 21         if check_vsftp_second != 0:
 22             print('Error: install vsftp faild, please check your system')
 23         else:
 24             print('vsftpd install successful!')
 25     else:
 26         print('vsftp already successful')
 27 
 28 def create_vsftp_conf():
 29     conf_dict = {
 30     'ftp_username':'nobody',
 31     'anonymous_enable':'YES',
 32     'local_enable':'YES',
 33     'write_enable':'YES',
 34     'local_umask':'022',
 35     'dirmessage_enable':'YES',
 36     'xferlog_enable':'YES',
 37     'connect_from_port_20':'YES',
 38     'xferlog_std_format':'YES',
 39     'chroot_local_user':'NO',
 40     'chroot_list_enable':'YES',
 41     'chroot_list_file':'/etc/vsftpd/chroot_list',
 42     'listen':'YES',
 43     'pam_service_name':'vsftpd',
 44     'userlist_enable':'YES',
 45     'tcp_wrappers':'YES',
 46     'anon_world_readable_only':'YES',
 47     'pasv_enable':'YES',
 48     'pasv_min_port':'10000',
 49     'pasv_max_port':'10010'}
 50     with open('/etc/vsftpd/vsftpd.conf','w') as conf_obj: 
 51         for key,val in conf_dict.items():
 52             line = key+"="+val
 53             conf_obj.write(line)
 54             conf_obj.write('\n')
 55 
 56 
 57 def create_ftp_user(user,pwd,user_passwd):
 58     res = subprocess.Popen(['ls -d %s' % pwd] ,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 59     stdout_val = res.stdout.read()
 60     stderr_val = res.stderr.read()
 61     if not stdout_val:
 62         print('Warning: "%s" 目录不存在! '% pwd)
 63         return
 64     else:
 65         if len(user) == 0 or len(user_passwd) == 0:
 66             print('用户和密码不能为空')
 67             return
 68         else:
 69             create_ftp_user = os.system('useradd -s /sbin/nologin -d %s %s' %(pwd,user))
 70             create_passwd = os.system('echo %s|passwd --stdin %s' %(user_passwd,user) )
 71             with open('/etc/vsftpd/chroot_list','a+') as chroot_list_obj:
 72                 chroot_list_obj.write(user)
 73                 chroot_list_obj.write('\n')
 74                 os.system('/etc/init.d/vsftpd restart')
 75 
 76 def create_vsftp_user(user,pwd,user_passwd):
 77     res = subprocess.Popen(['ls -d %s' % pwd] ,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 78     stdout_val = res.stdout.read()
 79     if not stdout_val:
 80         print('Warning: "%s" 目录不存在! '% pwd)
 81         return
 82     else:
 83         if len(user) == 0 or len(user_passwd) == 0:
 84             print('用户和密码不能为空')
 85             return
 86         else:
 87             create_vsftp_user = os.system('useradd  -d %s %s' %(pwd,user))
 88             create_passwd = os.system('echo %s|passwd --stdin %s' %(user_passwd,user) )
 89             with open('/etc/vsftpd/chroot_list','a+') as chroot_list_obj:
 90                 chroot_list_obj.write(user)
 91                 chroot_list_obj.write('\n')
 92                 os.system('/etc/init.d/vsftpd restart')
 93 
 94 def del_user(user):
 95     res = subprocess.Popen(['id %s' % user] ,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 96     stdout_val = res.stdout.read()
 97     if len(stdout_val) == 0:
 98         print('您删除的用户不存在')
 99     else:
100         print('''
101 删除用户时,将会删除该用户的Home目录,请确认是否删除
102 删除:yes
103 不删除:NO''')
104         inp = input("请输入您的选择:").strip()
105         if inp == 'yes':
106             print("即将删除用户 %s" % user)
107             os.system('userdel -r %s' % user)
108         else:
109             print("撤销删除用户")
110             return
111 
112 if __name__ == '__main__':
113     while True:
114         print('''
115 +++++++++++++++++++++++++
116 请选择:                +
117 1.新建vsftp服务器       +    
118 2.创建ftp用户           +
119 3.创建vsftp用户         +
120 4.删除用户              +
121 5.退出                  +
122 +++++++++++++++++++++++++''')
123         your_choice = input("请选择编号:").strip()
124         if your_choice == '1':
125             install_vsftp()
126             create_vsftp_conf()
127             continue
128         if your_choice == '2':
129             user = input('请输入用户名:').strip()
130             pwd = input('请输入 Home 目录:').strip()
131             user_passwd = getpass.getpass('请输入用户密码:')
132             create_ftp_user(user,pwd,user_passwd)
133             continue
134         if your_choice == '3':
135             user = input('请输入用户名:').strip()
136             pwd = input('请输入 Home 目录:').strip()
137             user_passwd = getpass.getpass('请输入用户密码:')
138             create_vsftp_user(user,pwd,user_passwd)
139             continue
140         if your_choice == '4':
141             user = input('请输入将要删除的用户名:').strip()
142             del_user(user)
143         if your_choice == '5':
144             print('欢迎下次使用')
145             break

 

posted @ 2017-05-18 16:32  Nice_keep-going  阅读(415)  评论(0编辑  收藏  举报