How can I set a users password in linux from a python script?

后端 未结 5 1780
陌清茗
陌清茗 2020-12-09 00:54

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:



        
5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 01:06

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

提交回复
热议问题