-1

So I am writing a script in python that inputs commands directly to the terminal of an Ubuntu machine. I use the os.system function to input the command to get super user access and I am having a bit of trouble entering the password into the script and getting super user privileges. Here's my code:

import os

def runUpdates():
    suPassword = input("Enter su password: ")
    su = os.system("sudo su")
    su2 = os.system(suPassword)
    return su
    return su2

If anyone can help that would be appreciated. Sorry if this is too vague of a question.

bengouma
  • 1
  • 2

1 Answers1

1
import os                                                                       

def runUpdates():                                                               
    suPassword = input("Enter su password: ")                                   
    os.popen("sudo su - %s date", 'w').write(suPassword)
runUpdates()  

(date is a command)

Other way using subprocess

import subprocess
import getpass

password = getpass.getpass()
p = subprocess.Popen(['sudo', '-p', '-k', '-S', 'date'],                     
                    stdout=subprocess.PIPE,
                    stdin=subprocess.PIPE,                                  
                    stderr=subprocess.STDOUT)
p.communicate(input='{0}\n'.format(password))[0]