【发布时间】:2013-08-12 00:20:28
【问题描述】:
我正在python 中制作一个 webshell,所以实际上,用户将通过 web 服务器使用他最喜欢的 shell。我的想法是用bash -i创建一个subprocess.Popen,并在webapp中创建两个函数read和write,分别读取stdout或写入subprocess的stdin。
我使用以下命令启动 shell:
p = subprocess.Popen(["script","-f","-c","bash -i -l"],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
文字还可以,但是当我阅读标准输出时,我没有看到用户输入的内容:
while select.select([p.stdout],[],[],0)[0]!=[] or select.select([p.stderr],[],[],0)[0]!=[]:
if(select.select([p.stdout],[],[],0)[0]!=[]): data+=p.stdout.read(1)
if(select.select([p.stderr],[],[],0)[0]!=[]): data+=p.stderr.read(1)
我可以强制回显将用户输入添加到输出中,但这不是很优雅,因为如果用户使用一些阻止回显的程序(如密码输入),用户输入将始终显示在网页中。
那么,有一种方法,比如bash 参数中的一个选项,可以强制将输入添加到输出中?
PS:如果你想知道为什么我使用script 运行bash 是因为单独运行bash 会导致python 自行停止
[1]+ Stopped python RPCServer.py
虽然我还没有弄清楚它为什么会发生,但我已经找到了如何防止它出现这个问题:Main Python Process is stopped using subprocess calls in SocketServer
【问题讨论】: