如何使用python在linux中创build一个用户

如何使用Python在Linux中创build用户? 我的意思是,我知道subprocess模块,并想到调用“adduser”并同时传递所有参数,但“adduser”命令会提供一些问题,如密码,全名,电话和东西。 我将如何使用子stream程回答这个问题? 我在这个问题中看到了一个名为pexpect的模块: 我可以使用Python作为Bashreplace吗? 。 还有其他的标准模块吗?

使用useradd ,它不会提出任何问题,但接受许多命令行选项。

在Ubuntu上,你可以使用python-libuser软件包

你可以使用内置的二进制文件,所以只需要通过子进程模块调用useradd或者其他东西。但是我不知道是否有任何其他的模块挂钩到Linux来提供这样的功能。

 import os import crypt password ="p@ssw0rd" encPass = crypt.crypt(password,"22") os.system("useradd -p "+encPass+" johnsmith") 
 def createUser(name,username,password): encPass = crypt.crypt(password,"22") return os.system("useradd -p "+encPass+ " -s "+ "/bin/bash "+ "-d "+ "/home/" + username+ " -m "+ " -c \""+ name+"\" " + username)