更改密码,python,linux

发布于 2024-11-01 19:09:28 字数 41 浏览 6 评论 0 原文

如何通过python脚本更改ubuntu root用户的密码?谢谢。

How can i change password of ubuntu root user by python script? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假装不在乎 2024-11-08 19:09:28

有两种主要方法可以解决此问题 -

一种是调用 passwd 命令行工具python(例如通过 stdlib 的 subprocess 模块)。如果您的脚本不是以 root 身份运行,则需要使用“su”或“sudo”命令进行换行才能提升到 root 权限。将预期数据写入标准输入应该足够了,但如果您发现需要根据 sudo/passwd 提示的内容执行不同的操作,pexpect 模块可能会有所帮助。

第二种是直接写入存储密码哈希值的 /etc/shadow 文件。这肯定需要您的脚本以 root 身份运行,以便对 /etc/shadow 具有读/写权限。 Stdlib 提供了 spwd 模块来访问 /etc/shadow,但它是只读的,所以你必须推出自己的阅读器/编写器...... csv 模块可能是有用,/etc/shadow 是接近为带有“:”分隔符的 csv 文件,但有一些细微的差别。

如果您选择第二条路线,您将需要能够生成替换密码的新哈希值,并将它们插入影子文件中。 Linux 上最快的方法是使用 stdlib crypt 模块,但你必须处理盐生成,并设置适当的密码哈希前缀(“$5$”、“$6$”等)。或者,Passlib 库中的 host_context 对象可以为您处理大部分事情(免责声明:我是该库的作者)。

一般来说,如果可能的话,我会推荐第一种路线 - 直接修改 /etc/shadow 充满危险 - 如果你搞乱了 /etc/shadow 文件,你将无法登录。如果你走这条路线,大量备份文件

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

The second is writing directly to the /etc/shadow file where the password hashes are stored. This will definitely require your script to run as root, in order to have read/write perms on /etc/shadow. 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.

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

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.

微凉 2024-11-08 19:09:28

您可以使用需要 root 权限的 Python 脚本修改 /etc/passwd (/etc/shadow) sudo pythonmodify.py /etc/passwd (其中 modify.py 是将更改密码的脚本)

You can modify /etc/passwd (/etc/shadow) with Python script which will need root permissions sudo python modify.py /etc/passwd (where modify.py is your script that will change password)

浴红衣 2024-11-08 19:09:28

您可以使用 commands 模块将输出通过管道传输到终端。

x = commands.getstatusoutput("passwd root")

但是,您必须发挥创意尝试输入“旧密码:”和“新密码:”的值。在命令完成之前,变量 x 不会被分配,并且在输入旧密码和新密码之前,命令不会完成。如果您只是第二次使用命令模块,那么它只会生成一个新的子进程。因此,就像其他人所说的那样,只需使用 open 写入 /etc/shadow功能。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文