24

我想自动响应某些程序提示的某些问题,例如 mysql 提示输入密码,或 apt 询问“是”或......当我想用 ./manage.py rebuild_index 重建我的 haystack 索引时.

对于 MySQL,我可以使用 --password= 开关,而且我确信 apt 有一个类似“安静”的选项。但是我怎样才能将响应传递给其他程序呢?

4

6 回答 6

39

如果您正在寻找用户来确认操作,请使用 confrim 方法。

if fabric.contrib.console.confirm("You tests failed do you want to continue?"):
  #continue processing

或者,如果您正在寻找一种从用户那里获取输入的方法,请使用 prompt 方法。

password = fabric.operations.prompt("What is your password?")
于 2010-02-11T17:50:29.110 回答
13

为什么不能只使用管道

例如,对于自动自动接受,只需使用yes,它只会输出一个永无止境的y.

yes | rm *.txt


(来源:wikimedia.org

于 2010-02-11T17:47:37.887 回答
1

Fabric(1.0a)的开发版本现在支持与远程程序的交互。 http://docs.fabfile.org/1.0a/usage/interactivity.html

于 2011-02-15T10:05:00.923 回答
1

这两种方法都是有效的并且有效。

我选择第一个,因为我不想与我的部署系统有任何交互。

所以这是我使用的解决方案:

% yes | ./manage.py rebuild_index

WARNING: This will irreparably remove EVERYTHING from your search index. Your choices after this are to restore from backups or rebuild via the rebuild_index command. Are you sure you wish to continue? [y/N] Removing all documents from your index because you said so. All documents removed. Indexing 27 Items.

于 2010-02-11T18:03:55.560 回答
0

迟到的答案,但希望这能帮助有类似问题的人。

不同点:

  1. 向控制台 回答两个或多个不同的输入。
  2. 并行模式支持。
  3. 包括任何类型的输入yes/no/y/n

问题

[hostxxx] out: Type 'c' if you want to use the Commercial Edition.
[hostxxx] out: Type 'o' if you want to use the Open Source Edition.
[hostxxx] out: Type '3' to view the GNU General Public License version 3.
[hostxxx] out: Type 'L' to view the Lesser GNU General Public License version 2.1.
[hostxxx] out: Type 'yes' to accept this license offer.
[hostxxx] out: Type 'no' to decline this license offer.

解决方案:

使用printf而不是yes增加更多灵活性,同时这就像parallel模式上的魅力。

@parallel
def demo_multi_input():
    run('printf "o\nyes\n"|./configure --prefix=/home/work/bin/qt')
于 2018-01-26T04:49:02.473 回答
-1

使用此代码:

run("echo yes|./manage.py rebuild_index")
于 2017-09-29T10:42:41.900 回答