普遍的问题是,我想使用pexpect调用需要sudo权限的脚本,但我并不总是想输入密码(仅输入一次)。
我的计划是使用pexpect生成具有sudo权限的bash会话并从那里调用脚本。基本上,我总是想保持会话繁忙,每当一个脚本停止时,我都想启动另一个。但是,当脚本运行时,我希望用户可以控制。含义:
应该在诸如Expect(“ root @”)之类的名称之后调用脚本,因此,无论会话何时空闲,它都会启动另一个脚本。在脚本运行时,interact()使用户可以控制他想要提供的输入。
我的想法是使用不同的线程来解决此问题。我的代码(用于概念验证)如下所示:
importpexpectimportthreadingclassBashInteractThread(threading.Thread):def__init__(self,process):threading.Thread.__init__(self)self.pproc=processdefrun(self):self.pproc.interact()s=pexpect.spawn("/bin/bash",['-i','-c',"sudo bash"])it=BashInteractThread(s)it.start()s.expect("root@")s.sendline("cd ~")while(s.isalive()):passs.close()
当我调用此脚本时,它没有任何输出,但是该过程似乎已经开始。不过,我无法按CTRL-C或CTRL-D终止该进程-我必须分别终止该进程。我期望的行为是,提示您输入密码,然后它将自动将目录更改为主目录。我不完全知道为什么它不起作用,但是我猜输出只能转发给interact()或Expect()。
有谁知道如何解决这个问题?提前致谢。
解决方案
您可以利用interact(output_filter=func)。我只是写了一个简单的示例(没有编码风格!)。它的作用是生成一个Bashshell,并反复调用Python以供用户进行交互。要退出陷阱,只需输入(或打印)魔术词LET ME OUT。
expect()之后将不再工作interact(),因此需要手动进行模式匹配。
代码:
[STEP 101] # cat interact_with_filter.py
import pexpect, re
def output_filter(s):
global proc, bash_prompt, filter_buf, filter_buf_size, let_me_out
filter_buf += s
filter_buf = filter_buf[-filter_buf_size:]
if "LET ME OUT" in filter_buf:
let_me_out = True
if bash_prompt.search(filter_buf):
if let_me_out:
proc.sendline('exit')
proc.expect(pexpect.EOF)
proc.wait()
else:
proc.sendline('python')
return s
filter_buf = ''
filter_buf_size = 256
let_me_out = False
bash_prompt = re.compile('bash-[.0-9]+[$#] $')
proc = pexpect.spawn('bash --noprofile --norc')
proc.interact(output_filter=output_filter)
print "BYE"
[STEP 102] #
让我们尝试一下:
[STEP 102] # python interact_with_filter.py
bash-4.4# python
Python 2.7.9 (default, Jun 29 2016, 13:08:31)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
bash-4.4# python
Python 2.7.9 (default, Jun 29 2016, 13:08:31)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
bash-4.4# python
Python 2.7.9 (default, Jun 29 2016, 13:08:31)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> LET ME OUT
File "", line 1
LET ME OUT
^
SyntaxError: invalid syntax
>>> exit()
bash-4.4# BYE
[STEP 103] #