Not able to pipe through a inputpassword in python

164 views Asked by At

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
0

There are 0 answers