Reputation: 19
I am attempting to execute sudo from a python script:
import os
command = 'id'
os.popen("sudo -S %s"%(command), 'w').write('password')
My server is configured to not allow non-TTY shells to execute sudo for security reasons, is there a workaround for this to execute the sudo command using python (without using su).
The above code outputs:
sudo: sorry, you must have a tty to run sudo
Upvotes: 1
Views: 591
Reputation: 13189
Based on this: https://stackoverflow.com/a/21444480/1216776 you should be able to do:
import os
import pty
command = 'id'
scmd ="sudo -S %s"%(command)
def reader(fd):
return os.read(fd)
def writer(fd):
yield 'password'
yield ''
pty.spawn(scmd, reader, writer)
Upvotes: 1