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

查看:814
本文介绍了如何从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是我希望系统运行的命令, 用户是从列表中获取的,是我要为其更改密码的用户,而newpass是我想分配给用户"的密码

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\n%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.

我已经在Internet和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!

谢谢.

推荐答案

我今天遇到了同样的问题,我在subprocess周围编写了一个简单的包装程序,以调用passwd命令并使用新密码输入stdin .该代码不是万无一失的,仅当以root身份运行时才起作用,而不会提示输入旧密码.

I ran accross the same problem today and I wrote a simple wrapper around subprocess to call the passwd command and feed stdin with the new password. This code is not fool proof and only works when running as root which does not prompt for the old password.

import subprocess
from time import sleep

PASSWD_CMD='/usr/bin/passwd'

def set_password(user, password):
    cmd = [PASSWD_CMD, user]
    p = subprocess.Popen(cmd, stdin=subprocess.PIPE)
    p.stdin.write(u'%(p)s\n%(p)s\n' % { 'p': password })
    p.stdin.flush()
    # Give `passwd` cmd 1 second to finish and kill it otherwise.
    for x in range(0, 10):
        if p.poll() is not None:
            break
        sleep(0.1)
    else:
        p.terminate()
        sleep(1)
        p.kill()
        raise RuntimeError('Setting password failed. '
                '`passwd` process did not terminate.')
    if p.returncode != 0:
        raise RuntimeError('`passwd` failed: %d' % p.returncode)

如果需要passwd的输出,还可以将stdout=subprocess.PIPE传递给Popen调用并从中读取.就我而言,我只对操作是否成功感兴趣,所以我只是跳过了这一部分.

If you need the output of passwd you can also pass stdout=subprocess.PIPE to the Popen call and read from it. In my case I was only interested if the operation succeeded or not so I simply skipped that part.

安全注意事项:请勿使用echo -n 'password\npassword\n | passwd username'之类的东西,因为这会使密码在进程列表中可见.

Security consideration: Do not use something like echo -n 'password\npassword\n | passwd username' as this will make the password visible in the process list.

由于您希望使用sudo passwd <username>,因此建议您在/etc/sudoers中添加新行(为此使用visudo!)

Since you seam to want to be using sudo passwd <username> I would recommend adding a new line to your /etc/sudoers (use visudo for that!)

some_user ALL = NOPASSWD: /usr/bin/passwd

Sudo不会要求输入some_user的密码,脚本将按预期运行.

Sudo will not ask for the password for some_user and the script will run as expected.

或者简单地添加一条额外的p.stdin.write(u'%s\n' % SUDO_PASSWORD)行.这样,sudo将首先接收用户密码,然后passwd将接收新的用户密码.

Alternatively simply add an extra p.stdin.write(u'%s\n' % SUDO_PASSWORD) line. That way sudo will receive the user password first and then passwd receives the new user password.

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

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