Python 用户创建脚本

标签 python

出于培训原因,我正在尝试编写一个 python 脚本来创建和设置用户帐户和密码:

import subprocess
from subprocess import Popen

users = ["user1"]
default_passwd = 'password'

for user in users:
    p1 = subprocess.Popen(["useradd" ,user])
    proc = Popen(['echo' ,default_passwd ,  '|' , 'passwd', user, '--stdin'])
    proc.communicate()

创建用户时,passwd 过程失败。 任何帮助将不胜感激。

最佳答案

为什么不在命令 useradd 中传递密码? 这样它就可以在不提示的情况下创建一个带密码的用户!!

import os
import crypt

password ="your-password" 
crypted_password = crypt.crypt(password,"22")
os.system("useradd -p "+ crypted_password +" student")

关于Python 用户创建脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46464037/

相关文章:

Python自省(introspection): get the argument list of a method_descriptor?

Python 从函数内部更改外部变量

python - 类型错误 : unsupported operand type(s) for/: 'instance' and 'instance' and __truediv__/__div__ differences?

python - 列表中的重复元素

python - 获取 "TypeError: ' 模块的对象不可调用"与 `socket.socket()`

python - 如何在Python中找到以下矩阵的行列式? A = np.array([[1 -'a' -'y' ,'a' ], ['b' , 'y' ]])

Python urllib2 图像扭曲

python - 是否可以在 Excel 中使用 Python 动态更改公式引用的路径?

python - 我需要帮助让我的物体四处移动

python - 对单元格范围进行排序,win32com.client