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



我想使用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'没有捕获。它只在输入密码后返回。

是否有一种方法可以自动执行这样的交互式提示?

可以使用提示符

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

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

最新更新