pexpect on Windows, sending text to input-hidden field

50 Views Asked by At

I'm trying to use python's pexpect library to interface with a command called fei5kinit, which creates a persistent authentication credential for a File Exchange Interface. On Windows and macOS, the process of simply running the command fei5kinit in the Command Prompt / Terminal looks like the following:

$ fei5kinit
Server group>>YOUR_GROUP
User name>>YOUR_USERNAME
Passcode>>YOUR_PASSCODE   <== the inputted passcode is invisible
$ 

With the right credentials, the command will finish with no additional ouput; the Command Prompt / Terminal will return to the standard input character, and the persistent authentication session will have been created. Notably, the Passcode>> input field is hidden, and when you type, no characters appear there.

On macOS, using python 3.11+, I have succeeded in capturing this entire process as follows:

# First, set up the fei5 command line tools (proprietary) and set the environment variable
import pexpect
child = pexpect.spawn('fei5kinit')
child.expect('Server group>>')
child.sendline('YOUR_GROUP') # Example input
child.expect('User name>>') 
child.sendline('YOUR_USERNAME') # Example input
child.expect('Passcode>>')
child.sendline('YOUR_PASSWORD') # Example input
index = child.expect(pexpect.EOF, "Authentication Error") # Check for valid login
if index == 0:
    print('Authentication Complete')
else:
    print('Error')
# >>> Authentication Complete

After running this code, I am able to run other commands (like fei5list and fei5get) with a simple call to subprocess.Popen, and they behave as expected.

However, I am running into an issue when trying to do the same thing on Windows. Because pexpect is supported directly on Windows, I have replaced the line child = pexpect.spawn('fei5kinit') with child = pexpect.popen_spawn.PopenSpawn('fei5kinit'). The rest of the code remains the same. From what I can tell, the code works just fine to send the Server group and User name, but fails when it hits the Passcode field.

I suspect that it might have to do with the fact that the Passcode field is hidden, and PopenSpawn() is not equivalent to spawn() in terms of ability to mimic user input, which might be required to interface with hidden fields. But that is just my theory. In addition to what I've described above, I have tried various approaches using pywinpty, winpexpect, and pywinauto to no avail. I have also tried using subprocess with no luck. The closest I have gotten is by spawning the fei5kinit command with pexpect.popen_spawn.PopenSpawn(). I am running Windows 11.

I would appreciate any tips or ideas, including suggestions for a better formulation of my question. I have been stuck on this for weeks. Thank you!

0

There are 0 best solutions below