how to type sudo password when using subprocess.call?

前端 未结 2 1438
栀梦
栀梦 2020-12-17 01:04

i defined a function that switch my proxy settings every now and then, problem is that i want it to run in a loop without manual intervention. But when i execute the program

2条回答
  •  粉色の甜心
    2020-12-17 01:58

    Another method of passing your password to a shell command through python that wouldn't involve it showing up in any command history or ps output is:

    p = subprocess.Popen(['sudo', self.resubscribe_script], stdin=subprocess.PIPE)
    p.communicate('{}\n'.format(self.sudo_password))
    

    Note that using communicate will only allow one input to be given to stdin; there are other methods for getting a reusable input.

提交回复
热议问题