<bdo id='Q9pPE'></bdo><ul id='Q9pPE'></ul>

      <small id='Q9pPE'></small><noframes id='Q9pPE'>

    1. <i id='Q9pPE'><tr id='Q9pPE'><dt id='Q9pPE'><q id='Q9pPE'><span id='Q9pPE'><b id='Q9pPE'><form id='Q9pPE'><ins id='Q9pPE'></ins><ul id='Q9pPE'></ul><sub id='Q9pPE'></sub></form><legend id='Q9pPE'></legend><bdo id='Q9pPE'><pre id='Q9pPE'><center id='Q9pPE'></center></pre></bdo></b><th id='Q9pPE'></th></span></q></dt></tr></i><div id='Q9pPE'><tfoot id='Q9pPE'></tfoot><dl id='Q9pPE'><fieldset id='Q9pPE'></fieldset></dl></div>
      <tfoot id='Q9pPE'></tfoot>
    2. <legend id='Q9pPE'><style id='Q9pPE'><dir id='Q9pPE'><q id='Q9pPE'></q></dir></style></legend>

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

        时间:2023-07-21

            • <bdo id='2xo36'></bdo><ul id='2xo36'></ul>

              <tfoot id='2xo36'></tfoot>
              <i id='2xo36'><tr id='2xo36'><dt id='2xo36'><q id='2xo36'><span id='2xo36'><b id='2xo36'><form id='2xo36'><ins id='2xo36'></ins><ul id='2xo36'></ul><sub id='2xo36'></sub></form><legend id='2xo36'></legend><bdo id='2xo36'><pre id='2xo36'><center id='2xo36'></center></pre></bdo></b><th id='2xo36'></th></span></q></dt></tr></i><div id='2xo36'><tfoot id='2xo36'></tfoot><dl id='2xo36'><fieldset id='2xo36'></fieldset></dl></div>

              <small id='2xo36'></small><noframes id='2xo36'>

                <tbody id='2xo36'></tbody>
                1. <legend id='2xo36'><style id='2xo36'><dir id='2xo36'><q id='2xo36'></q></dir></style></legend>

                  本文介绍了如何通过 python 脚本在 linux 中设置用户密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

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

                  我可以这样创建用户:

                  >>> 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')
                  

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

                  In my python program it has no effect, in the interactive interpreter it locks up after the first line.

                  最好的方法是什么?

                  我在 Ubuntu lucid 上运行 python 2.6.

                  I'm running python 2.6 on Ubuntu lucid.

                  推荐答案

                  communicate 的文档说你需要添加 stdin=PIPE 如果你是通过 communicate 参数将数据发送到标准输入:

                  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:

                  • 如果您对 useradd 命令的输出不感兴趣,而不是它是否失败,您最好使用 subprocess.check_call 会引发如果命令返回非零,则异常.
                  • 在第二种情况下,您应该在调用 communicate('test:password')
                  • 后检查 process.returncode 是否为 0
                  • 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')

                  这篇关于如何通过 python 脚本在 linux 中设置用户密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用python将本地文件推送到github?(或通过 Python 发布提交) 下一篇:如果stdout = PIPE,我如何找出为什么subprocess.Popen wait() 永远等待?

                  相关文章

                    1. <i id='WPapk'><tr id='WPapk'><dt id='WPapk'><q id='WPapk'><span id='WPapk'><b id='WPapk'><form id='WPapk'><ins id='WPapk'></ins><ul id='WPapk'></ul><sub id='WPapk'></sub></form><legend id='WPapk'></legend><bdo id='WPapk'><pre id='WPapk'><center id='WPapk'></center></pre></bdo></b><th id='WPapk'></th></span></q></dt></tr></i><div id='WPapk'><tfoot id='WPapk'></tfoot><dl id='WPapk'><fieldset id='WPapk'></fieldset></dl></div>
                      <legend id='WPapk'><style id='WPapk'><dir id='WPapk'><q id='WPapk'></q></dir></style></legend>

                      <small id='WPapk'></small><noframes id='WPapk'>

                      <tfoot id='WPapk'></tfoot>
                        <bdo id='WPapk'></bdo><ul id='WPapk'></ul>