How to automate shell input with python?
Question: I am automating some tasks with python, but have hit a bit of a roadblock. One
of the tasks I am automating requires user input in the shell.

The requirement is that you to run the command with an email address as a
parameter (simple enough), and then you are asked to authenticate with the
password for that email address. How can you simulate user input to provide
the password?

There are also some menus afterwards which ask options, for which the input
need just be to repeatedly hit enter. How is this simulated? Keeping in mind
that this window will not always have focus..


Answer: I'm not sure what you're asking in the second part, but subprocesses can be
controlled with the `pexpect` module. For example:

    
    
    #!/usr/bin/env python
    
    import pexpect
    import sys
    
    # Get email and password somehow
    #email = ...
    #password = ...
    
    # Start the subprocess
    child = pexpect.spawn('mycommand %s' % email)
    # redirect output to stdout
    child.logfile_read = sys.stdout
    
    # Assumes the prompt is "password:"
    child.expect('password:')
    child.sendline(password)
    
    # Wait for the process to close its output
    child.expect(pexpect.EOF)
    

