如何让 Fabric 自动(而不是用户交互)与 shell 命令交互?与 pexpect 结合?

发布于 2024-12-18 07:59:46 字数 815 浏览 6 评论 0原文

Seeking 意味着让 Fabric 自动(而不是用户交互)与 shell 命令交互(而不仅仅是请求密码,但当没有诸如 apt-get install -y 之类的“stdin/交互式覆盖”可用时,也会请求用户输入)。

这个问题以及这些Fabric 文档表明 Fabric 只能将交互性“推送”回运行 Fabric 程序的人类用户。寻求在无人参与的情况下实现完全自动化。目前还没有“真正的”问题需要解决,只是为未来可能出现的障碍做好准备。

如果 Fabric 不能独占,则与 pexpect (或类似的替代机制)结合使用可能有用自动处理所有标准输入/提示?希望它不需要成为“非此即彼”之类的事情。如果适用的话,为什么不在同一程序/自动化中适当地利用两者(pexpect 和 Fabric)?

Seeking means to get Fabric to automatically (instead of user-interactively) interact with shell commands (and not just requests for passwords, but also requested user input when no "stdin/interactive override" like apt-get install -y is available).

This question along with these Fabric docs suggest that Fabric can only "push the interactivity" back to the human user that's running the Fabric program. Seeking to instead fully automate without any human presence. Don't yet have a "real," current problem to solve, just preparing for possible, future obstacle.

Possibly useful to combine with pexpect (or similar, alternative mechanism) if Fabric can't exclusively handle all stdin/prompts automatically? Hoping it doesn't need to be an "either/or" kind of thing. Why not leverage both (pexpect and Fabric) where appropriate, if applicable, in same program/automation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌生 2024-12-25 07:59:46

作为 Glenn,我会说使用 pexpect;另外,

看看我编写的用于编写 Fabric 的 pexpect 行为脚本的包装器:

from ilogue.fexpect import expect, expecting, run 

prompts = []
prompts += expect('What is your name?','John')
prompts += expect('Where do you live?','New York')

with expecting(prompts):
    run('command')

另请参阅我的博客文章 fexpect 或如何使用 pexpect 处理 Fabric 中的提示

As Glenn, I would say use pexpect; in addition,

have a look at this wrapper I wrote to script the pexpect behaviour from fabric:

from ilogue.fexpect import expect, expecting, run 

prompts = []
prompts += expect('What is your name?','John')
prompts += expect('Where do you live?','New York')

with expecting(prompts):
    run('command')

See also my blogpost on fexpect or how to handle prompts in fabric with pexpect

送你一个梦 2024-12-25 07:59:46

这不是非此即彼。您只需要通过 pexpect 运行 fab 命令:

child = pexpect.spawn('fab <task>')
child.expect('prompt:')
child.send('reponse to prompt')
... etc

fab 命令就像任何其他命令一样,因此可以通过 pexpect 编写脚本。

It's not either/or. You just need to run the fab command through pexpect:

child = pexpect.spawn('fab <task>')
child.expect('prompt:')
child.send('reponse to prompt')
... etc

The fab command is just like any other command, so it can be scripted through pexpect.

分开我的手 2024-12-25 07:59:46

对于 Windows 用户,请使用 winpexpect。确保使用我链接的这个版本,因为这个版本修复了以前版本中的一些错误。

import sys, winpexpect
child = winpexpect.winspawn('ftp', ['<ftp host>'])
child.logfile = sys.stdout
child.expect('User.*:')
child.sendline('username')
child.expect('Password:')
child.direct_sendline('password')
child .sendline('ls')
print('Now enter the FTP interactive mode')
child.interact()

For Windows users, use winpexpect. Make sure to use this version I linked as this version fixes some bugs in previous versions.

import sys, winpexpect
child = winpexpect.winspawn('ftp', ['<ftp host>'])
child.logfile = sys.stdout
child.expect('User.*:')
child.sendline('username')
child.expect('Password:')
child.direct_sendline('password')
child .sendline('ls')
print('Now enter the FTP interactive mode')
child.interact()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文