Python Forum
Subprocess command prompt (Windows)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Subprocess command prompt (Windows)
#1
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.
Reply
#2
You could perhaps try b"secret password\r\n"
Reply
#3
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:
Reply
#4
I meant p.stdin.write(b'secret password\r\n' * 2)
Reply
#5
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.
Reply
#6
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.
Reply
#7
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  'pip' not recogmnized in windows prompt MaartenRo 1 362 Dec-21-2023, 09:05 AM
Last Post: menator01
  What is the difference between Command Prompt and Sublimes yaoyao22 1 588 Jul-09-2023, 02:56 PM
Last Post: snippsat
  Using subprocess to execute complex command with many arguments medatib531 5 1,719 Apr-27-2023, 02:23 PM
Last Post: medatib531
  [SOLVED] [Windows] Right way to prompt for directory? Winfried 4 1,996 Jan-17-2023, 09:28 PM
Last Post: markoberk
  Open windows cmd prompt and run cmds with python Extra 3 1,403 Jul-14-2022, 06:07 AM
Last Post: Gribouillis
  Facing Problem while opening a file through command prompt vlearner 4 1,857 Jan-30-2022, 08:10 AM
Last Post: snippsat
  use subprocess on linux\pi wwith a "grep " command korenron 2 7,906 Oct-19-2021, 10:52 AM
Last Post: DeaD_EyE
  disable subprocess.popen prompt echo paul18fr 1 1,966 Feb-04-2021, 02:50 AM
Last Post: Larz60+
  Noob warning: trying to use pip to install pytest and pep8 in Command Prompt adifrank 4 5,231 Dec-20-2020, 04:23 AM
Last Post: adifrank
  Running Python in Command Prompt Anwar 3 3,002 Nov-15-2020, 03:15 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020