如何使用python自动执行shell输入?

我正在使用python自动执行一些任务,但遇到了一些障碍.我要自动化的任务之一是需要用户在shell中输入.
要求是使用电子邮件地址作为参数(足够简单)运行命令,然后要求您使用该电子邮件地址的密码进行身份验证.您如何模拟用户输入以提供密码?
后面还有一些菜单会询问选项,输入只是重复按回车即可.如何模拟?请记住,此窗口将不会始终具有焦点.
解决方法:
我不确定在第二部分中您要问什么,但是可以使用pexpect模块控制子流程.例如:
#!/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)