【发布时间】:2012-01-03 10:27:09
【问题描述】:
使用 python pty 模块,我想向终端仿真器发送一些命令,使用一个函数作为标准输入(如 pty 模块想要的那样),然后强制退出。我想到了类似的东西
import pty
cmnds = ['exit\n', 'ls -al\n']
# Command to send. I try exiting as last command, but it doesn't works.
def r(fd):
if cmnds:
cmnds.pop()
# It seems is not executing sent commands ('ls -al\n')
else:
# Can i quit here? Can i return EOF?
pass
pty.spawn('/bin/sh', r)
谢谢
【问题讨论】:
-
您可能想要调查 pexpect,它是为这类事情设计的:noah.org/wiki/pexpect
-
我刚刚尝试过但无法使其工作,但从文档中我得到的功能是与输出交互而不是接收参数列表
-
那么没有办法将字符串作为标准输入发送?