Using sudo with Python script

前端 未结 11 2183
轻奢々
轻奢々 2020-11-22 15:10

I\'m trying to write a small script to mount a VirtualBox shared folder each time I execute the script. I want to do it with Python, because I\'m trying to learn it for scri

11条回答
  •  无人及你
    2020-11-22 16:12

    • Use -S option in the sudo command which tells to read the password from 'stdin' instead of the terminal device.

    • Tell Popen to read stdin from PIPE.

    • Send the Password to the stdin PIPE of the process by using it as an argument to communicate method. Do not forget to add a new line character, '\n', at the end of the password.

    sp = Popen(cmd , shell=True, stdin=PIPE)
    out, err = sp.communicate(_user_pass+'\n')   
    

提交回复
热议问题