简体   繁体   English

安全问题:通过python更改系统密码

[英]security question: Changing system password via python

I'm working on a project aimed at system administration for a linux installation. 我正在针对Linux安装进行系统管理的项目。

I need to perform some tasks like change the user password... 我需要执行一些任务,例如更改用户密码...

I was planning to use the subprocess module for this. 我打算为此使用子流程模块。 I'm concerned about security so, what are the 'best practices' when doing this via python? 我担心安全性,通过python执行此操作时,“最佳做法”是什么?

is subprocess sufficient, or is there something better out there for the job? 子流程是否足够,还是有更好的工作条件?

[EDIT] [编辑]

I should add that this is not an interactive script, it will parse the values to the system's passwd program [/EDIT] 我应该补充一点,这不是交互式脚本,它将解析值到系统的passwd程序[/ EDIT]

I believe the pexpect module would be the easiest way to go about this. 我相信pexpect模块将是解决此问题的最简单方法。

http://pexpect.sourceforge.net/pexpect.html http://pexpect.sourceforge.net/pexpect.html

Something along these lines should work pretty well: 这些方面的工作应该很好:

import pexpect
import time

def ChangePassword(user, pass):
    passwd = pexpect.spawn("/usr/bin/passwd %s" % user)

    for x in xrange(2):
        # wait for password: to come out of passwd's stdout
        passwd.expect("password: ")
        # send pass to passwd's stdin
        passwd.sendline(pass)
        time.sleep(0.1)

ChangePassword('foo', 'bar') # changes user "foo"'s password to "bar"

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

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