2

I develop a python gui for a c program. The Program asks for two passwords. How can I wait for the password promt and then insert the password in code?

At the moment I try to do this via:

subprocess.popen.communicate(input)

But that doesn't work as the program asks for the password in the shell again.

Just for your understanding here is the flow of the program:

  • shell: execute the program with some parameters
  • stdout: "please insert pw 1"
  • stdin: pw1
  • stdout: "please insert pw 2"
  • stdin: pw2
Flo
  • 2,699
  • 4
  • 24
  • 46
  • Possible duplicate of [unable to provide password to a process with subprocess \[python\]](http://stackoverflow.com/questions/25189348/unable-to-provide-password-to-a-process-with-subprocess-python) – Jaimin Ajmeri Jan 17 '17 at 10:00

1 Answers1

2

You should considerate using another library : Pexpect

Pexpect is a pure Python module for spawning child applications; controlling them; and responding to expected patterns in their output. Pexpect works like Don Libes’ Expect. Pexpect allows your script to spawn a child application and control it as if a human were typing commands.

Here is a sample of code to give a username and password when connecting to FTP server :

import pexpect
child = pexpect.spawn('ftp ftp.openbsd.org')
child.expect('Name .*: ')
child.sendline('bob')
child.expect('Password:')
child.sendline('thisisnotasecurepassword')
iFlo
  • 1,442
  • 10
  • 19