简体   繁体   English

更改密码,Python,Linux

[英]Changing password, python, linux

How can i change password of ubuntu root user by python script? 如何通过python脚本更改ubuntu根用户的密码? Thanks. 谢谢。

There are two main ways to go about this - 有两种主要方法可以解决此问题-

One is calling the passwd command line tool from python (such as via stdlib's subprocess module). 一种是从python调用passwd命令行工具(例如通过stdlib的subprocess模块)。 If your script isn't running as root, you'll need to wrap using the "su" or "sudo" commands in order to elevate to root privledge. 如果您的脚本不是以root身份运行,则需要使用“ su”或“ sudo”命令进行换行,以提升为root特权。 Writing the expected data to stdin should be sufficient, but if you find you need to perform different actions based on exactly what the sudo/passwd prompts say, the pexpect module may be helpful. 将期望的数据写入stdin应该足够,但是如果您发现您需要根据sudo / passwd提示的确切执行不同的操作,则pexpect模块可能会有所帮助。

The second is writing directly to the /etc/shadow file where the password hashes are stored. 第二种是直接写入存储密码哈希的/ etc / shadow文件。 This will definitely require your script to run as root, in order to have read/write perms on /etc/shadow. 为了在/ etc / shadow上具有读/写权限,这肯定需要您的脚本以root用户身份运行。 Stdlib offers the spwd module for accessing /etc/shadow, but it's read-only, so you'll have to roll your own reader/writer... the csv module might be useful, /etc/shadow is close to being a csv file with a ":" separator, but with some minor differences. Stdlib提供了用于访问/ etc / shadow的spwd模块,但是它是只读的,因此您必须滚动自己的读取器/写入器... csv模块可能很有用,/ etc / shadow 接近于 csv带有“:”分隔符的文件,但有一些细微差别。

If you choose the second route, you'll need to be able to generate new hashes of replacement password, and insert them into the shadow file. 如果选择第二种方法,则需要能够生成新的替换密码哈希,并将其插入到影子文件中。 The fastest way on linux is to use the stdlib crypt module, but you'll have to take care of salt generation, and setting the appropriate password hash prefix ("$5$", "$6$" etc). 在Linux上最快的方法是使用stdlib crypt模块,但是您必须注意生成盐,并设置适当的密码哈希前缀(“ $ 5 $”,“ $ 6 $”等)。 Alternately, the host_context object in the Passlib library can take care of most of that for you (disclaimer: I'm the author of that library). 另外,Passlib库中的host_context对象可以为您解决大部分问题(免责声明:我是该库的作者)。

In general, I'd recommend the first route if possible - modifying /etc/shadow directly is fraught with danger - if you mess up the /etc/shadow file, you won't be able to log in. If you go this route, back up the file a lot . 通常,如果可能的话,我建议第一种方法-直接修改/ etc / shadow充满危险-如果弄乱了/ etc / shadow文件,将无法登录。 , 大量备份文件

您可以使用需要根权限的Python脚本修改/etc/passwd/etc/shadowsudo python modify.py /etc/passwd (其中modify.py是您的脚本,它将更改密码)

You can use the commands module to pipe output to the terminal. 您可以使用命令模块将输出通过管道传输到终端。

x = commands.getstatusoutput("passwd root")

However, you'll have to get creative trying to enter the values for "Old Password:" and "New Password:." 但是,您必须要有创意,尝试输入“旧密码:”和“新密码:”的值。 The variable x wont be assigned until the command is finished, and the command won't finish until the old and new passwords are entered. 直到命令完成,才分配变量x,直到输入新密码和新密码,命令才会完成。 If you just use the command module a second time, then it will simply spawn a new subprocess. 如果您仅第二次使用命令模块,那么它将仅产生一个新的子进程。 So, like others have said, just write to /etc/shadow using the open function. 因此,就像其他人所说的那样,只需使用open函数写入/ etc / shadow。

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

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