0

I am trying to pass this pipe stnd input from a script password.py. this script excepts a password from user. but it is not allowing me to see by piping the input through a screen from subprocess.

I want to use subprocess to automate the process because I don't want to enter the password/key every time.

password.py

import getpass

password = getpass.getpass("Enter your password: ")
if password != "test":
    print("wrong password")
else:
    print("correct password")

main.py

import subprocess as sp

# Start the "password.py" script as a subprocess
proc = sp.Popen(["python", "password.py"], stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)

# Read the process's standard output and error streams
stdout, stderr = proc.communicate()

# Print the output
print(stdout)
print(stderr)

# Wait for the process to complete and get its return code
returncode = proc.wait()

# Check the return code
if returncode == 0:
    print("Command successful")
else:
    print("Command failed")

Error:

b'/usr/lib/python3.8/getpass.py:91: GetPassWarning: Can not control echo on the terminal.\n  

passwd = fallback_getpass(prompt, stream)\nWarning: Password input may be echoed.\nEnter your password: Traceback (most recent call last):\n  

File "/usr/lib/python3.8/getpass.py", line 69, in unix_getpass\n  
  old = termios.tcgetattr(fd)     # a copy to save\ntermios.error: (25, \'Inappropriate ioctl for device\')\n\n

During handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  

File "password.py", line 3, in <module>\n    password = getpass.getpass("Enter your password: ")\n  

File "/usr/lib/python3.8/getpass.py", line 91, in unix_getpass\n    passwd = fallback_getpass(prompt, stream)\n  

File "/usr/lib/python3.8/getpass.py", line 126, in fallback_getpass\n    return _raw_input(prompt, stream)\n  

File "/usr/lib/python3.8/getpass.py", line 148, in _raw_input\n    raise EOFError\nEOFError\n'
Command failed
  • Just try without hide_input=True (the password will be displayed like: ******* – kithuto Dec 28 '22 at 09:25
  • 1
    You are reinventing `subprocess.check_output`, poorly. See also `subprocess.run` and note how the `Popen` documentation encourages you to use these in favor of the raw low-level `Popen` when you can. – tripleee Dec 28 '22 at 09:35
  • I'm guessing the subprocess gets a different environment which causes it to `import` a different version of `pyinputplus` which doesn't recognize this keyword. A good fix would be to not use a subprocess at all. – tripleee Dec 28 '22 at 09:37
  • 1
    @NachoR. kindly see the edit in question. after removing hide_input=True, this happens – Rabia Hussain Dec 28 '22 at 09:45
  • 1
    @tripleee after using subprocess.run(["python", "password.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) the error is the same, when I remove hide_input=True, the error pasted in the edited part – Rabia Hussain Dec 28 '22 at 09:55
  • @tripleee what should I use instead of subprocess? – Rabia Hussain Dec 28 '22 at 09:57
  • Refactor the password script into a function you can `import` and call. See e.g. https://stackoverflow.com/a/69778466/874188 (the topic is tangential but it explains the idea). This should also fix the `ioctl` problem which happens because the subprocess is spawned incorrectly for your use case. – tripleee Dec 28 '22 at 10:49

0 Answers0