how to type sudo password when using subprocess.call?

前端 未结 2 1435
栀梦
栀梦 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.

    0 讨论(0)
  • 2020-12-17 01:59

    Here you can execute a command sudo without interactive prompt asking you to type your password :

    from subprocess import call    
    
    pwd='my password'
    cmd='ls'
    
    call('echo {} | sudo -S {}'.format(pwd, cmd), shell=True)
    
    0 讨论(0)
提交回复
热议问题