[英]How can I set a users password in linux from a python script?
I'm trying to automate the setup of SFTP access. 我正在尝试自动设置SFTP访问。 This script is running as a user with sudo permissions and no password.
此脚本作为具有sudo权限且没有密码的用户运行。
I can create a user like so: 我可以像这样创建一个用户:
>>> import subprocess
>>> process = subprocess.Popen(['sudo', 'useradd', 'test'], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> process.communicate()
('', '')
Next I need to set the user's password, but I can't figure out how. 接下来我需要设置用户的密码,但我无法弄清楚如何。 Here's what I've tried.
这是我尝试过的。
>>> process = subprocess.Popen(['sudo', 'chpasswd'], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> process.communicate('test:password')
In my python program it has no effect, in the interactive interpreter it locks up after the first line. 在我的python程序中它没有任何效果,在交互式解释器中它在第一行之后锁定。
What's the best way to do this? 最好的方法是什么?
I'm running python 2.6 on Ubuntu lucid. 我在Ubuntu lucid上运行python 2.6。
Try below code which will do as you required automation 尝试以下代码,这将按照您的要求自动执行
from subprocess import Popen, PIPE, check_call
check_call(['useradd', 'test'])
proc=Popen(['passwd', 'test'],stdin=PIPE,stdout=PIPE,stderr=PIPE)
proc.stdin.write('password\n')
proc.stdin.write('password')
proc.stdin.flush()
stdout,stderr = proc.communicate()
print stdout
print stderr
print
statements are optional. print
语句是可选的。
The documentation for communicate
says that you'll need to add stdin=PIPE
if you're sending data to standard input via the communicate
parameter: communicate
文档说如果您通过communicate
参数将数据发送到标准输入,则需要添加stdin=PIPE
:
http://docs.python.org/release/2.6/library/subprocess.html#subprocess.Popen.communicate http://docs.python.org/release/2.6/library/subprocess.html#subprocess.Popen.communicate
I appreciate this is just skeleton code, but here are another couple of other small comments, in case they are of use: 我很欣赏这只是一个骷髅代码,但这里有另外两个小评论,如果它们有用:
useradd
command other than whether it failed or not, you might be better off using subprocess.check_call
which will raise an exception if the command returns non-zero. useradd
命令的输出不感兴趣,除了它是否失败,你可能最好使用subprocess.check_call
,如果命令返回非零,则会引发异常。 process.returncode
is 0 after your call to communicate('test:password')
communicate('test:password')
后检查process.returncode
是否为0 communicate('test:password')
On Ubuntu, use usermod 在Ubuntu上,使用usermod
class SomeClass
def userPasswd(self, login, password):
encPass = crypt.crypt(password)
command = "usermod -p '{0:s}' {1:s}".format(encPass, login)
result = os.system(command)
if result != 0:
logging.error(command)
return result
我想问题是你忘记了sudo的-S选项。
You forgot this: 你忘了这个:
stdin=subprocess.PIPE
To send data to the process, you need a stdin
. 要将数据发送到进程,您需要一个
stdin
。
So the full statement is: 所以完整的陈述是:
process = subprocess.Popen(['sudo', 'chpasswd'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
and then call communicate('password')
. 然后调用
communicate('password')
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.