def execute_with_prompt(command, prompt, response, timeout=-1):
    '''Execute a system command which expects a single prompt (e.g. for a
    password) to be answered by the user before running.

    A timeout may be specified in seconds, where a value of -1 (a Pexpect
    convention) indicates that the default timeout (30 seconds) should be
    used.

    The result is a generator which produces each line of output as it is
    received from the command. This is especially advantageous for
    long-running commands or commands with a great deal of output, since the
    caller does not have to wait for the command to finish before receiving
    output, and the output will not take up memory as it would if collected
    into a list.

    Trailing CRLFs (included with each line coming from a pty) are stripped
    from each line.

    A simple example:

        for line in execute_with_prompt('sudo cat /etc/shadow', 'Password:', 'secret'):
            print line.split(':')[0:2]
    '''
    child = pexpect.spawn(command)
    child.expect(prompt, timeout=timeout)
    child.sendline(response)

    # compile a regular expression to find trailing CRLFs. note that we can't
    # simply use .rstrip() because it would strip -all- whitespace from the
    # end of each line, which could be inappropriate in some use cases.
    trailing_crlf = re.compile('\r\n$')

    # return a generator (via a generator expression) which will produce the
    # next line of output with each iteration.
    return (trailing_crlf.sub('', line) for line in child)
Execute a system command which expects a single prompt (e.g. for a
    password) to be answered by the user before running.

    A timeout may be specified in seconds, where a value of -1 (a Pexpect
    convention) indicates that the default timeout (30 seconds) should be
    used.

    The result is a generator which produces each line of output as it is
    received from the command. This is especially advantageous for
    long-running commands or commands with a great deal of output, since the
    caller does not have to wait for the command to finish before receiving
    output, and the output will not take up memory as it would if collected
    into a list.

    Trailing CRLFs (included with each line coming from a pty) are stripped
    from each line.

    A simple example:

        for line in execute_with_prompt('sudo cat /etc/shadow', 'Password:', 'secret'):
            print line.split(':')[0:2]
    
recipes/Python/572207_Using_Pexpect_netrc_automate_commands_which/recipe-572207.py