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

前端 未结 3 509
不知归路
不知归路 2020-12-05 05:18

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

相关标签:
3条回答
  • 2020-12-05 05:42

    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

    0 讨论(0)
  • 2020-12-05 05:51

    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()
    
    0 讨论(0)
  • 2020-12-05 06:02

    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.

    0 讨论(0)
提交回复
热议问题