Python Forum

Full Version: Subprocess command prompt (Windows)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I'm making a little program that runs a cmd command on Windows. That command needs a password and a verification of the password from the user to be typed it in the terminal. What I want to do is to send the password myself from the program to the terminal. The program looks something like this:

from subprocess import *
p = Popen("Command that does something and asks  for a password", stdin=PIPE, stdout=PIPE, shell=True)
I have tried different solutions but none of them have worked. I've tried this:

p.communicate(b"secret password\n") #I have tried with and without the \n
And also this:

p.stdin.write("secret password")
I don't get any error message, but the terminal still asks me for password.
You could perhaps try b"secret password\r\n"
That doesn't seem to work neither.

from subprocess import *

p = Popen("Command that does something and asks  for a password",stdin=PIPE, stdout=PIPE, shell=True)
p.communicate(b"secret password\r\n")
Output:
C:\>python subprocess_test.py Enter password: Verify password:
I meant p.stdin.write(b'secret password\r\n' * 2)
I tried:
from subprocess import *

p = Popen('Command that does something and asks for a password', stdin=PIPE, stdout=PIPE, shell=True)
p.stdin.write(b"secret password\r\n" * 2)
Output:
C:\>python subprocess_test.py The process tried to write to a nonexistent pipe. The process tried to write to a nonexistent pipe. The process tried to write to a nonexistent pipe. The process tried to write to a nonexistent pipe. The process tried to write to a nonexistent pipe. ....
The answer I've got was that line repeated over and over a bunch of times.
I changed it a bit adding flush() but I still can't figure it to work.
This is the code:
from subprocess import *

p = Popen(['cmd', '/k', 'Command that does something and asks for a password'], stdin=PIPE)
p.stdin.write(b'test\r\n' *2)
p.stdin.flush()
I also removed the shell=True because it's not recommended in the python subprocess docs. If you remove it you need to add 'cmd', '/k' to make it work. The /k will open the terminal and run the command or you can run /c to just run it without opening a terminal.

It still asks me for password and to verify it so it's not working yet.
I'm still looking to make this work. If anybody has a suggestion or can help me it would be very appreciated. Thanks in advance

-
Arnau