在 pexpect 中同时使用 expect() 和 interact()

And*_*dré 3 python-2.x pexpect

一般的问题是,我想使用 pexpect 调用需要 sudo 权限的脚本,但我并不总是想输入我的密码(只输入一次)。

我的计划是使用 pexpect 生成具有 sudo 权限的 bash 会话并从那里调用脚本。基本上我总是想让会话保持忙碌,每当一个脚本停止时,我想启动另一个。但是在脚本运行时,我希望用户拥有控制权。意义:

脚本应该在诸如 expect("root@") 之类的之后调用,因此只要会话空闲,它就会启动另一个脚本。当脚本运行时,interact() 让用户控制他想要提供的可能输入。

我的想法是使用不同的线程来解决这个问题。我的代码(用于概念验证)如下所示:

import pexpect
import threading

class BashInteractThread(threading.Thread):
    def __init__(self, process):
        threading.Thread.__init__(self)
        self.pproc = process

    def run(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()):
    pass


s.close()
Run Code Online (Sandbox Code Playgroud)

当我调用这个脚本时,它没有给我任何输出,但这个过程似乎已经开始。尽管如此,我还是无法使用 CTRL-C 或 CTRL-D 来终止该进程——我必须单独终止该进程。我期望的行为是提示输入密码,然后它应该自动将目录更改为主目录。我不完全知道为什么它不起作用,但我想输出只会转发到交互()或期望()。

有没有人知道如何解决这个问题?提前致谢。

pyn*_*exj 5

您可以利用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] #
Run Code Online (Sandbox Code Playgroud)

让我们试试看:

[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()          <---- user input
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()          <---- user input
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      <---- user input
  File "<stdin>", line 1
    LET ME OUT
         ^
SyntaxError: invalid syntax
>>> exit()          <---- user input
bash-4.4# BYE
[STEP 103] #
Run Code Online (Sandbox Code Playgroud)