Python Fabric捕获交互式对话框输出

问题描述 投票:1回答:2

我想使用Fabric为远程服务器上的用户设置密码。

假设我有一个名为'john'的用户,我想为他设置默认密码'123'。

理想情况下,我想这样做:

run 'passwd john' on remote machine
detect when linux prompts for 'Enter new UNIX password:'
automatically enters the password
detect when linux prompts for 'Retype new UNIX password:'
automatically reenters the password

这是我尝试过的:

result = run('passwd {}'.format(username))

该语句的问题在于,当Linux提示输入密码时,不会捕获'result'。仅在输入密码后返回。

有没有办法像这样自动化交互提示?

python fabric
2个回答
0
投票

您可以使用prompts

password = '123'
with settings(prompts={
                  'Enter new UNIX password: ': password,
                  'Retype new UNIX password: ': password
              }):
    run('passwd {}'.format(username))

0
投票

您可以使用fexpect进行交互式提示。

© www.soinside.com 2019 - 2024. All rights reserved.