1

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
4

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.