Search code examples
pythonfabric

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.

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. It only returns after the passwords are entered.

Is there a way to automate interactive prompts like this?


Solution

  • you can use prompts

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