简体   繁体   中英

Subprocess function skips input for password and exits program

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.

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM