我遇到了一个需要在远程计算机上自动回答提示的要求,然后在阅读了不同的stackoverflow问题后发现了烦恼。但是,当我在脚本中包含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结构停止使用上述错误消息。

我也询问了irc,但是我知道这可能是由于某些与版本有关的问题。有人遇到过此错误吗?

fexpect == 0.2.post7
面料== 1.8.0

最佳答案

只需将fexpect的run导入为erun,将其sudo导入为esudo

使用fexpect runsudo函数时,必须将这些调用包装在with expecting(prompts):上下文中。确切地说,这是一个known issue,尽管有一个拉取请求,所以它可能会在后代读取此请求时被修复。

一种解决方案是使用其他名称导入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示例代码对此产生了误导。

关于python - fexpect破坏了结构脚本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20283680/

10-13 04:43