0

I'm calling subprocess.run to execute an external program outside. However the program requires administrative rights, I run the program using administrator but the python console prompts me for a password, but doesn't let me input the password and exits the program.

I've tried using subprocess.popen and subprocess.call, I've also tried running the program without administrative rights but pycharm will throw me a operation requires elevation error.

def runExecutables():
    directory = r"C:\Users\Billy\Desktop\SURVEY_PROGRAM_WINDOWS_ENGLISH.exe"
    #subprocess.Popen(directory)

    subprocess.run(['runas', '/user:Administrator', directory])
    #prog.stdin.write(b'password')
    #prog.communicate()

I should be expecting, either the executable to run, or a prompt that pops up asking for the password to be inputted, proving to me that the executable is indeed being run. I am just getting a python prompt to enter the pass for administrator and it does not wait for me to enter the password before finishing the process.

Anwarvic
  • 12,156
  • 4
  • 49
  • 69
BChiu
  • 69
  • 1
  • 10

2 Answers2

0

With Popen, you have to pipe in stdin and flush the input

import subprocess

p = subprocess.Popen(['runas', '/user:Administrator', 'calc.exe'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write(b'YourPassword\n')
p.stdin.flush()
stdout, stderr = p.communicate()
print("-----------")
print(stdout)
print("-----------")
print(stderr)
HariUserX
  • 1,341
  • 1
  • 9
  • 17
  • that solution, doesn't execute the program when I run it in pycharm. It still prints the prompt of entering the password except this time it spits out random characters after the prompt. – BChiu Jan 25 '19 at 20:04
  • have to tried executing it from cmd prompt first. See if "runas /user:Administrator C:\Users\Billy\Desktop\SURVEY_PROGRAM_WINDOWS_ENGLISH.exe" works fine in your cmd prompt – HariUserX Jan 25 '19 at 20:07
  • yea i ran the program in cmd, i did runas, i got the prompt for the password, and the executable was run – BChiu Jan 25 '19 at 20:16
0

I have avoided this problem by approaching it using command prompt rather than using subprocess.

def runExecutables():
     os.system(r"start C:\Users\Mastodon\Desktop\SURVEY_PROGRAM_WINDOWS_ENGLISH.exe")

Using command prompt alleviates some of the problems that subprocess would inflict. I am unclear as to what the advantages of using subprocess are.

BChiu
  • 69
  • 1
  • 10
  • 1
    Have a look [here](https://stackoverflow.com/questions/4813238/difference-between-subprocess-popen-and-os-system) or [here](https://stackoverflow.com/questions/44730935/advantages-of-subprocess-over-os-system) or at the [docs](https://docs.python.org/3/library/os.html#os.system) which recommend using subprocess rather than os.system, explaining why. – Valentino Jan 28 '19 at 21:48