How to get Fabric to automatically (instead of user-interactively) interact with shell commands? Combine with pexpect?

♀尐吖头ヾ 提交于 2019-12-28 05:28:45

问题


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?


回答1:


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




回答2:


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.




回答3:


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()


来源:https://stackoverflow.com/questions/8291380/how-to-get-fabric-to-automatically-instead-of-user-interactively-interact-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!