Python结构捕获交互式对话框outpu

2024-06-10 14:24:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用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”。它只在输入密码后返回。你知道吗

有没有一种方法可以自动执行这样的交互式提示?你知道吗


Tags: therun用户密码newforlinuxunix
2条回答

您可以使用fexpect进行交互式提示。你知道吗

你可以使用prompts

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

相关问题 更多 >