简体   繁体   English

使用Python(sftp)更改远程Linux系统的密码

[英]Changing password of a remote Linux system using Python (sftp)

It seems very simple, but I searched multiple resources but could not find an answer on how to change a remote Linux system password using Python and with SFTP. 这似乎很简单,但我搜索了多个资源,但无法找到如何使用Python和SFTP更改远程Linux系统密码的答案。

def changepwd():
    sftp_client = ssh.open_sftp()
    #change password of root on remote server

Are there any built-in modules that I can use to change the password? 是否有可用于更改密码的内置模块? Thanks in advance. 提前致谢。


Thanks for all you help. 谢谢你的帮助。 This is how I changed the passwd for 'root'. 这就是我为'root'更改passwd的方法。

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, username=Name, password=Pwd)
print "Connection succesfully established ...with %s " % hostname

stdin, stdout, stderr = ssh.exec_command('echo -e "newpasswd\newPasswd" | passwd')
print "stderr: ", stderr.readlines()
print "pwd: ", stdout.readlines()
ssh.close()

You cannot change password with SFTP protocol. 您无法使用SFTP协议更改密码。

You can change password with SSH protocol. 您可以使用SSH协议更改密码。 But the SSH protocol API for changing a password is not support by the most widespread SSH server – OpenSSH. 但是,用于更改密码的SSH协议API不受最广泛的SSH服务器 - OpenSSH的支持。 Nor it is supported by the most widespread Python SSH library – Paramiko. 它也不受最广泛的Python SSH库 - Paramiko的支持。 So this most likely won't work for you anyway. 所以这很可能对你无效。

So in the end the only viable option is to execute a relevant shell command ( passwd or chpasswd ) via SSH (eg using Paramiko). 所以最后唯一可行的选择是通过SSH执行相关的shell命令( passwdchpasswd )(例如使用Paramiko)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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