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

后端 未结 5 1781
陌清茗
陌清茗 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:13

    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.

提交回复
热议问题