Subprocess command prompt (Windows) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Subprocess command prompt (Windows) (/thread-12612.html) |
Subprocess command prompt (Windows) - arnaur - Sep-03-2018 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 \nAnd also this: p.stdin.write("secret password")I don't get any error message, but the terminal still asks me for password. RE: Subprocess command prompt (Windows) - Gribouillis - Sep-03-2018 You could perhaps try b"secret password\r\n"
RE: Subprocess command prompt (Windows) - arnaur - Sep-03-2018 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")
RE: Subprocess command prompt (Windows) - Gribouillis - Sep-03-2018 I meant p.stdin.write(b'secret password\r\n' * 2)
RE: Subprocess command prompt (Windows) - arnaur - Sep-03-2018 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) The answer I've got was that line repeated over and over a bunch of times.
RE: Subprocess command prompt (Windows) - arnaur - Sep-04-2018 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. RE: Subprocess command prompt (Windows) - arnaur - Sep-06-2018 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 |