Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 396
aemdy Avatar asked May 07 '12 08:05

aemdy


People also ask

What are Python prompts?

First steps. That >>> is called a prompt, which means it's something the computer displays to tell you it's ready for your instructions. You can type things into that window, and the computer will obey them when it understands your commands.

How do you use fabric in Python?

Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. Typical usage involves creating a Python module containing one or more functions, then executing them via the fab command-line tool.

What is fabric command?

More specifically, Fabric is: A tool that lets you execute arbitrary Python functions via the command line; A library of subroutines (built on top of a lower-level library) to make executing shell commands over SSH easy and Pythonic.


1 Answers

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') 
like image 121
Timothée Jeannin Avatar answered Sep 18 '22 11:09

Timothée Jeannin