I have a script that I want to run from within Python (2.6.5) that follows the logic below:
- Prompts the user for a password. It looks like ("Enter password: ") (*Note: Input does not echo to screen)
- Output irrelevant information
- Prompt the user for a response ("Blah Blah filename.txt blah blah (Y/N)?: ")
The last prompt line contains text which I need to parse (filename.txt). The response provided doesn't matter (the program could actually exit here without providing one, as long as I can parse the line).
My requirements are somewhat similar to Wrapping an interactive command line application in a Python script, but the responses there seem a bit confusing, and mine still hangs even when the OP mentions that it doesn't for him.
Through looking around, I've come to the conclusion that subprocess
is the best way of doing this, but I'm having a few issues. Here is my Popen line:
p = subprocess.Popen("cmd", shell=True, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
When I call a
read()
orreadline()
onstdout
, the prompt is printer to the screen and it hangs.If I call a
write("password ")
forstdin
, the prompt is written to the screen and it hangs. The text inwrite()
is not written (I don't the cursor move the a new line).If I call
p.communicate("password ")
, same behavior as write()
I was looking for a few ideas here on the best way to input to stdin
and possibly how to parse the last line in the output if your feeling generous, though I could probably figure that out eventually.