touch my body touch my body - 5 months ago 56
Python Question

Print password prompt using Python's subprocess

If I run a terminal command from Python using

subprocess
, how do I print the password prompt?

I have a Python script that asks users to authenticate via Kerberos. My program is basically this:

import subprocess

def kinit():
kinit = "kinit -f"
prompt = subprocess.check_output(kinit, shell=True)
print prompt ## echoes "Password for username@domain.com" AFTER pw input

if __name__=="__main__":
kinit()
doOtherStuff()


When this script is launched from the Terminal, the program waits for password input (which can be input via the command line), but it only displays the "Password for username@domain.com" prompt AFTER the user inputs their password.

Do you know how I can display the "Password for username@domain.com" before the Python script waits for them to input their password?

Answer

Turns out you can't. subprocess will not return output in the middle of a process, and the process will not complete until you input user input.