起步
举个栗子
#!/usr/bin/env python
# coding=utf-8
import pexpect
cmd = "sudo touch sudo_touch"
passwd = '***'
run = pexpect.spawn(cmd)
try:
run.sendline(passwd)
ouput = run.read()
print ouput
except:
print 'Error'
pexpect.spawn('sudo touch sudo_touch').sendline("***")
函数解释
run()函数
from pexpect import *
(command_output, exitstatus) = run ('ls -l /bin', withexitstatus=1)
spawn()类
class spawn:
def __init__(self,command,args=[],timeout=30,maxread=2000, searchwindowsize=None, logfile=None, cwd=None, env=None)
child = pexpect.spawn ('/usr/bin/ftp', [])
child = pexpect.spawn ('/usr/bin/ssh', ['user@example.com'])
child = pexpect.spawn ('ls', ['-latr', '/tmp'])
记录日志
child = pexpect.spawn('some_command')
fout = file('mylog.txt','w')
child.logfile = fout
# 如果不需要记录向子程序输入的日志,只记录子程序的输出
child = pexpect.spawn('some_command')
child.logfile = sys.stdout
expect()函数
异常
try:
index = pexpect (['good', 'bad'])
if index == 0:
do_something()
elif index == 1:
do_something_else()
except EOF:
do_some_other_thing()
except TIMEOUT:
do_something_completely_different()
index = p.expect (['good', 'bad', pexpect.EOF, pexpect.TIMEOUT])
if index == 0:
do_something()
elif index == 1:
do_something_else()
elif index == 2:
do_some_other_thing()
elif index == 3:
do_something_completely_different()
send()函数
child.send('a')
child.sendline('bb')
child.sendcontrol('c') # 发送ctrl+c
结尾
#!/usr/bin/env python
# coding=utf-8
import pexpect
import random
class FtpAddUser():
ROOT_PASS = 'admin123' #sudo 密码
CROOT_LIST = '/etc/vsftpd.chroot_list' # 允许跨越目录
USER_DIR = '/etc/vsftp_user_conf/' # 用户相关配置文件夹
@staticmethod
def runCmdRoot(cmd, params = None):
run = pexpect.spawn(cmd)
try:
for line in params:
#time.sleep(0.5) # 休眠,确保以下输出是有效的
run.expect(':|:')
run.sendline(line)
except:
print 'Error'
return run.read()
@staticmethod
def create_password(length = 6, prex = ''):
word=[x for x in 'abcdefghijklmnopqrstuvwxyz0123456789']
return prex + ''.join(random.sample(word, length))
def __init__(self, username = None, password = None):
if username == None:
username = FtpAddUser.create_password(8, 'ftpuser')
if password == None:
password = FtpAddUser.create_password(8)
# 确保添加的用户名可用
while True:
cmd = "/bin/grep '^{}' /etc/passwd".format(username)
if FtpAddUser.runCmdRoot(cmd, []) == '': # 如果该用户名不存在,说明可添加
break
username = FtpAddUser.create_password(8, 'ftpuser')
self.username = username
self.password = password
def addUser(self):
params = [FtpAddUser.ROOT_PASS, self.password, self.password, '', '', '', '','', 'y']
#cmd = 'sudo adduser -g ftp -s /sbin/nologin {}'.format(self.username)
cmd = 'sudo adduser {}'.format(self.username)
FtpAddUser.runCmdRoot(cmd, params)
# 设置用户禁止登陆shell
cmd = 'sudo usermod -s /sbin/nologin {}'.format(self.username)
FtpAddUser.runCmdRoot(cmd, [FtpAddUser.ROOT_PASS,])
cmd = 'sudo usermod -g ftp {}'.format(self.username)
FtpAddUser.runCmdRoot(cmd, [FtpAddUser.ROOT_PASS,])
self.addUserConf()
def addUserConf(self):
cmd = "sudo touch /home/{}/这是{}用户默认的ftp目录.txt".format(self.username, self.username)
FtpAddUser.runCmdRoot(cmd, [FtpAddUser.ROOT_PASS,])
# 设置用户配置,登陆目录设为/home/username
with open('{}{}'.format(FtpAddUser.USER_DIR, self.username), 'w') as f:
f.write('local_root=/home/{}'.format(self.username))
#设置文件归属为root用户
cmd = 'sudo chown root:root {}{}'.format(FtpAddUser.USER_DIR, self.username)
FtpAddUser.runCmdRoot(cmd, [FtpAddUser.ROOT_PASS,])
def show(self):
print 'username is {}'.format(self.username)
print 'password is {}'.format(self.password)
if __name__ == "__main__":
test = FtpAddUser()
test.addUser()
test.show()