如何通过 python 脚本在 Linux 中设置用户密码?

发布于 2024-10-12 09:49:41 字数 648 浏览 4 评论 0原文

我正在尝试自动设置 SFTP 访问。该脚本以具有 sudo 权限但没有密码的用户身份运行。

我可以像这样创建一个用户:

>>> import subprocess
>>> process = subprocess.Popen(['sudo', 'useradd', 'test'], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> process.communicate()
('', '')

接下来我需要设置用户的密码,但我不知道如何设置。这是我尝试过的。

>>> process = subprocess.Popen(['sudo', 'chpasswd'], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> process.communicate('test:password')

在我的Python程序中它没有效果,在交互式解释器中它在第一行之后锁定。

最好的方法是什么?

我在 Ubuntu lucid 上运行 python 2.6。

I'm trying to automate the setup of SFTP access. This script is running as a user with sudo permissions and no password.

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.

What's the best way to do this?

I'm running python 2.6 on Ubuntu lucid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陪你到最终 2024-10-19 09:49:41

尝试下面的代码,它将按照您的要求执行自动化

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 语句是可选的。

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.

Saygoodbye 2024-10-19 09:49:41

communicate 的文档指出,如果您通过 communicate 参数将数据发送到标准输入,则需要添加 stdin=PIPE

http://docs.python.org/release/2.6 /library/subprocess.html#subprocess.Popen.communicate

我很欣赏这只是骨架代码,但这里还有另外几个其他小注释,以防它们有用:

  • 如果您对输出不感兴趣除了 useradd 命令是否失败之外,您可能最好使用 subprocess.check_call,如果命令返回非零,它会引发异常。
  • 在第二种情况下,您应该在调用 communicate('test:password') 后检查 process.returncode 是否为 0

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:

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:

  • If you're not interested in the output of the 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.
  • In the second case, you should check whether process.returncode is 0 after your call to communicate('test:password')
烟凡古楼 2024-10-19 09:49:41

在 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

On Ubuntu, use 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
落在眉间の轻吻 2024-10-19 09:49:41

您忘记了这一点:

stdin=subprocess.PIPE

要将数据发送到进程,您需要一个 stdin

所以完整的语句是:

process = subprocess.Popen(['sudo', 'chpasswd'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)

然后调用communicate('password')

You forgot this:

stdin=subprocess.PIPE

To send data to the process, you need a 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').

楠木可依 2024-10-19 09:49:41

我猜问题是你忘记了 sudo 的 -S 选项。

I guess the issue is that you forgot the -S option for sudo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文