1

我有一个需求,我需要自动回答远程机器上的提示,然后在阅读了不同的 stackoverflow 问题后我发现了 fexpect。但是,当我在脚本中包含 fexpect 时,它会破坏整个脚本!

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/fabric/main.py", line 743, in main
    *args, **kwargs
  File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 368, in execute
    multiprocessing
  File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 264, in _execute
    return task.run(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 171, in run
    return self.wrapped(*args, **kwargs)
  File "/etc/puppet/fabfile.py", line 165, in edit_sudoers
    run('echo "Current Permission of the file /etc/sudoers - "`stat -c "%a %n" /etc/sudoers`')
  File "/usr/local/lib/python2.7/dist-packages/ilogue/fexpect/api.py", line 15, in run
    wrappedCmd = wrapExpectations(cmd)
  File "/usr/local/lib/python2.7/dist-packages/ilogue/fexpect/internals.py", line 15, in wrapExpectations
    script = createScript(cmd)
  File "/usr/local/lib/python2.7/dist-packages/ilogue/fexpect/internals.py", line 39, in createScript
    for e in fabric.state.env.expectations:
  File "/usr/local/lib/python2.7/dist-packages/fabric/utils.py", line 184, in __getattr__
    raise AttributeError(key)
AttributeError: expectations

我写 from ilogue.fexpect import expect, expecting, run织物的那一刻停止使用上述错误消息。

我也在fabric irc中问过,但我知道这可能是因为一些版本相关的问题。以前有没有其他人遇到过这个错误?

fexpect==0.2.post7 Fabric==1.8.0

4

1 回答 1

3

只需导入 fexpect 的runaserun和它的sudoas esudo

当您使用 fexpectrunsudo函数时,您必须将这些调用包装在with expecting(prompts):上下文中。这是 fexpect 中的一个已知问题,尽管有一个拉取请求,所以它可能会在后人阅读此内容时得到修复。

一种解决方案是使用不同的名称导入 fexpect 的run函数,例如erun,并且仅在您需要自动提示处理功能时使用它:

from fabric.api import run 
from ilogue.fexpect import expect, expecting, run as erun
run(a_cmd)   # Native Fabric run - should work fine
prompts = [...]
with expecting(prompts):
    erun(a_prompting_cmd)   # fexpect run - should with fine inside expecting context

fexpect 文档中未明确说明的另一件事是该pexpect软件包需要安装在目标系统上。

另一个 fexpect 问题是提示字符串是正则表达式—— fexpect 示例代码对此具有误导性。

于 2013-12-03T20:31:25.703 回答