简体   繁体   中英

Python paramiko sending sudo password and other data to command

I want to change Ubuntu user password using ssh and Python.

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('xxx.xxx.xxx.xxx', username='xxx', password='xxx')
stdin,stdout,stderr = client.exec_command('sudo passwd test1')
stdin.write('passForSudo' + '\n')
stdin.write('newPassForUser' + '\n')
stdin.write('newPassForUser' + '\n')
stdin.flush()

Why this doesn't work? I can't find solution(

by default, sudo don't read password from stdin, but from terminal.

man sudo give :

...
-S, --stdin
             Write the prompt to the standard error and read the password from the standard
             input instead of using the terminal device.  The password must be followed by a
             newline character.

You should use this option in sudo.

best regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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