Kroll
Kroll

Reputation: 679

How to enter password on asyncssh.run("sudo su") and continue to input commands?

In the python 3 script I need to enter a few commands under root priveleges via asyncssh.

Something like this code sample:

async with asyncssh.connect(host=IP, port=PORT, username=ORDINARY_USER, password=PASSWORD, known_hosts=None) as conn:
    result_su = await conn.run('echo "verysecretpass" | su', check=False)
    result_1 = await conn.run("root_command1", check=False)
    print(result_1.stdout)
    result_2 = await conn.run("root_command2", check=False)
    print(result_2.stdout)

This sample is not working of course. What changes are needed to make it work?

Upvotes: 1

Views: 1428

Answers (1)

sotplug
sotplug

Reputation: 11

Instead of su you may use sudo with the -S switch which reads the password from STDIN:

echo <password> | sudo -S <command>

Credits to sudo with password in one command-line.

Upvotes: 1

Related Questions