【问题标题】:Python paramiko sending sudo password and other data to commandPython paramiko 向命令发送 sudo 密码和其他数据
【发布时间】:2015-07-15 14:21:28
【问题描述】:

我想使用 ssh 和 Python 更改 Ubuntu 用户密码。

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()

为什么这不起作用?我找不到解决方案(

【问题讨论】:

  • 它是如何失败的?它在哪条线上失败?您收到任何错误消息吗?他们说什么?

标签: python ssh paramiko


【解决方案1】:

默认情况下,sudo 不会从标准输入读取密码,而是从终端读取。

man sudo 给:

...
-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.

你应该在 sudo 中使用这个选项。

最好的问候

【讨论】: