如何从 python 更改 Linux 用户密码 [英] How to change a Linux user password from python

查看:24
本文介绍了如何从 python 更改 Linux 用户密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从 python 更改 Linux 用户密码时遇到问题.我尝试了很多东西,但我无法解决问题,以下是我已经尝试过的示例:

I'm having problems with changing a Linux user's password from python. I've tried so many things, but I couldn't manage to solve the issue, here is the sample of things I've already tried:

sudo_password 是 sudo 的密码,sudo_command 是我希望系统运行的命令,user 是从 List 中获取的,是我要更改密码的用户,newpass 是我要分配给user"的密码

sudo_password is the password for sudo, sudo_command is the command I want the system to run, user is get from a List and is the user who I want to change the password for, and newpass is the pass I want to assign to 'user'

    user = list.get(ANCHOR)
    sudo_command = 'passwd'
    f = open("passwordusu.tmp", "w")
    f.write("%s
%s" % (newpass, newpass))
    f.close()
    A=os.system('echo -e %s|sudo -S %s < %s %s' % (sudo_password, sudo_command,'passwordusu.tmp', user))
    print A
    windowpass.destroy()

'A'是os.system执行的返回值,本例是256.我也试过了

'A' is the return value for the execution of os.system, in this case 256. I tried also

    A=os.system('echo  %s|sudo -S %s < %s %s' % (sudo_password, sudo_command,'passwordusu.tmp', user))

但它返回相同的错误代码.我用passwd"命令尝试了其他几种方法,但都没有成功.使用 'chpasswd' 命令我已经尝试过:

but it returns the same error code. I tried several other ways with 'passwd' command, but whithout succes. With 'chpasswd' command I 've tried this:

    user = list.get(ANCHOR)
    sudo_command = 'chpasswd'
    f = open("passwordusu.tmp", "w")
    f.write("%s:%s" % (user, newpass))
    f.close()
    A=os.system('echo %s|sudo -S %s < %s %s' % (sudo_password, sudo_command,'passwordusu.tmp', user))
    print A
    windowpass.destroy()

还有:

    A=os.system('echo %s|sudo -S %s:%s|%s' % (sudo_password, user, newpass, sudo_command))
    @;which returns 32512
    A=os.system("echo %s | sudo -S %s < "%s"" % (sudo_password, sudo_command,  "passwordusu.tmp"))
    @;which returns 256

我也尝试过这样的mkpasswd"和usermod":

I tried 'mkpasswd' and 'usermod' too like this:

    user = list.get(ANCHOR)
    sudo_command = 'mkpasswd -m sha-512'
    os.system("echo %s | sudo -S %s %s > passwd.tmp" % (sudo_password,sudo_command, newpass))
    sudo_command="usermod -p"
    f = open('passwd.tmp', 'r')
    for line in f.readlines():
        newpassencryp=line
    f.close()
    A=os.system("echo %s | sudo -S %s %s %s" % (sudo_password, sudo_command, newpassencryp, user))
    @;which returns 32512

但是,如果你去 https://www.mkpasswd.net ,散列 'newpass' 和代替'newpassencryp',它返回0,理论上意味着它已经正确,但到目前为止它并没有改变密码.

but, if you go to https://www.mkpasswd.net , hash the 'newpass' and substitute for 'newpassencryp', it returns 0 which theoretically means it has gone right, but so far it doesn't changes the password.

我已经在互联网和 stackoverflow 上搜索过这个问题或类似问题,并尝试了暴露的解决方案,但同样没有成功.

I've searched on internet and stackoverflow for this issue or similar and tried what solutions exposed, but again,without success.

我真的很感激任何帮助,当然,如果您需要更多信息,我很乐意提供!

I would really apreciate any help, and of course, if you need more info i'll be glad to supply it!

提前致谢.

推荐答案

运行此命令的用户必须具有 sudo 权限才能在没有密码的情况下运行 passwd 命令.

The user you are running this as must have sudo permission to run the passwd command without a password.

>>> from subprocess import Popen
>>> proc = Popen(['/usr/bin/sudo', '/usr/bin/passwd', 'test', '--stdin'])
>>> proc.communicate('newpassword')

这篇关于如何从 python 更改 Linux 用户密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆