How to answer to prompts automatically with python

2019-01-08 11:29发布

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?

6条回答
太酷不给撩
2楼-- · 2019-01-08 12:05

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')
查看更多
【Aperson】
3楼-- · 2019-01-08 12:21

Putting this as an answer though its a comment from @BobNadler

run("yes | my_command");

查看更多
劫难
4楼-- · 2019-01-08 12:22

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

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-08 12:23

To expand a bit on Timothée's excellent answer, here's the code that Fabric uses when checking the prompts dictionary.

def _get_prompt_response(self):
    """
    Iterate through the request prompts dict and return the response and
    original request if we find a match
    """
    for tup in env.prompts.iteritems():
        if _endswith(self.capture, tup[0]):
            return tup
    return None, None

Fabric uses .endswith for its check, so make sure you include trailing spaces in the string you use as a key in the prompts dictionary.

For example - let's say you are trying to automate the Django test database prompt

Type 'yes' if you would like to try deleting the test database 'test_my_app', or 'no' to cancel:

All we need is enough of the end of the prompt so that it is unique. Include trailing spaces.

django_test_database_prompt = "or 'no' to cancel: "
#         won't work without this trailing space ^

with settings(
    prompts={django_test_database_prompt : 'yes'}
):
    run('%s %s' % (virtualenv_python_path,
                   test_runner_file_path,
                  )
       )
查看更多
劳资没心,怎么记你
6楼-- · 2019-01-08 12:27

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

run('echo "yes\n"| my_command')
查看更多
看我几分像从前
7楼-- · 2019-01-08 12:29

In Fabric 2.1, this can be accomplished using the auto-respond example that is available through the invoke package (a dependency of Fabric 2.1):

>>> from invoke import Responder
>>> from fabric import Connection
>>> c = Connection('host')
>>> sudopass = Responder(
...     pattern=r'\[sudo\] password:',
...     response='mypassword\n',
... )
>>> c.run('sudo whoami', pty=True, watchers=[sudopass])
[sudo] password:
root
<Result cmd='sudo whoami' exited=0>

Note that this is not limited to sudo passwords and can be used anywhere where you have a pattern to match for and a canned response (that may not be a password).

There are a couple of tips:

  1. pty=True is probably always necessary
  2. The pattern specified within the Responder can often include spaces at the end of the line so try adding spaces when the watcher doesn't seem to match.
  3. According to the note discussed at the end of the watcher docs:

    The pattern argument to Responder is treated as a regular expression, requiring more care (note how we had to escape our square-brackets in the above example) but providing more power as well.

    So, don't forget to escape (using backslashes) where necessary.

查看更多
登录 后发表回答