简体   繁体   English

Python构造捕获交互式对话框输出

[英]Python fabric capture interactive dialog output

I would like to use fabric to set the password for a user on a remote server. 我想使用结构为远程服务器上的用户设置密码。

Let's say I have a user named 'john' and I want to set a default password '123' for him. 假设我有一个名为“ john”的用户,我想为他设置一个默认密码“ 123”。

Ideally I would like to do this: 理想情况下,我想这样做:

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

This is what I have tried: 这是我尝试过的:

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

The problem with this statement is that 'result' does not capture when linux prompts for entering password. 该语句的问题在于,当Linux提示输入密码时,不会捕获“结果”。 It only returns after the passwords are entered. 仅在输入密码后返回。

Is there a way to automate interactive prompts like this? 有没有办法自动化这样的交互式提示?

Thanks! 谢谢!

您可以将fexpect用于交互式提示。

you can use prompts 您可以使用提示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM