Martin Rezyne
Martin Rezyne

Reputation: 445

Python: Give input for a subprocess

I have to create a new process with Admin privileges using a python script. I searched on the Internet about this topic and found out that I have to use runas command. For this command, I have to give an user and at runtime, the script will wait for the password and I want to do this as automatic as possible.

This is my code:

import subprocess

p = subprocess.Popen(['runas', '/user:Armando', szCmdCommand], stdin = subprocess.PIPE)
p.stdin.write('password')

Instead of .write I tried p.communicate(input='password') but it isn't working because it doesnt write the password. The only thing that happens is that the script doesn't wait for input. It automatically takes a wrong password and fails the execution of my command.

What can I do to fix this?

Upvotes: 1

Views: 1774

Answers (1)

Messa
Messa

Reputation: 25181

Programs that expect passwords to be entered usually do not use simple read from standard input (stdin) for this, but they control the terminal directly (as you can see in source code of the getpass module).

Use something other, for example:

Upvotes: 1

Related Questions