How to answer to prompts automatically with python fabric? How to answer to prompts automatically with python fabric? python python

How to answer to prompts automatically with python fabric?


Starting from version 1.9, Fabric includes a way of managing this properly.

The section about Prompts in the Fabric documentation says:

The prompts dictionary allows users to control interactive prompts. If a key in the dictionary is found in a command’s standard output stream, Fabric will automatically answer with the corresponding dictionary value.

You should be able to make Fabric automatically answer prompts like this:

with settings(prompts={'Do you want to continue [Y/n]? ': 'Y'}):    run('apt-get update')    run('apt-get upgrade')


I have used simple echo pipes to answer prompts with Fabric.

run('echo "yes\n"| my_command')


Note: this answer is several years old, and in the mean time fabric has (interestingly similar looking) implementation of this. See the answer by @timothée-jeannin below.

See https://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