aemdy aemdy - 19 days ago 18
Python Question

How to answer to prompts automatically with python fabric?

I want to run a command which prompts me to enter yes/no or y/n or whatever. If I just run the command

local("my_command")
then it stops and asks me for input. When I type what is needed, script continues to work. How can I automatically respond to the prompt?

Answer

See http://stackoverflow.com/a/10007635/708221

pip install fexpect

from ilogue.fexpect import expect, expecting, run 

prompts = []
prompts += expect('What is your name?','John')
prompts += expect('Are you at stackoverflow?','Yes')

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

Fexpect adds answering to prompts to fabric with use of pexpect