I am attempting to automate a Java tool invocation that requires two separate password inputs during its execution. The tool is pepk.jar
, used for key encryption, and it prompts for the keystore password twice. I am trying to automate this using Python's subprocess
module, but neither of the two methods I've attempted has worked successfully.
The Java tool needs to receive one password input, wait for me to press enter, then receive the second password input, followed by another enter. In a manual process, I would type a password, press enter, type the password again, and press enter once more.
I tried the following two methods with subprocess
:
Method 1:
pythonimport subprocesscommand = ['java', '-jar', 'pepk.jar', 'arg1', 'arg2', 'etc']process = subprocess.Popen(command, stdin=subprocess.PIPE, text=True)process.communicate(input='password')process.communicate(input='password')import subprocesscommand = ['java', '-jar', 'pepk.jar', 'arg1', 'arg2', 'etc']proc = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)proc.stdin.write(f'{password}\n')proc.stdin.write(f'{password}\n')proc.stdin.flush()stdout, stderr = proc.communicate()print(stdout)print(stderr)