我想使用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'。仅在输入密码后返回。
有没有办法像这样自动化交互提示?
您可以使用prompts
password = '123'
with settings(prompts={
'Enter new UNIX password: ': password,
'Retype new UNIX password: ': password
}):
run('passwd {}'.format(username))
您可以使用fexpect
进行交互式提示。