强制在管道中执行bash以启用回音

2024-04-25 22:59:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在python中制作一个webshell,所以实际上,用户将通过web服务器使用他最喜欢的shell。我的想法是用bash -i创建一个subprocess.Popen,并在webapp中生成两个函数readwrite,分别读取stdout或写入subprocessstdin。你知道吗

我从以下内容开始:

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


Tags: 方法用户bashreaddataifstderrstdin
1条回答
网友
1楼 · 发布于 2024-04-25 22:59:40

So, there is a way, like an option in the bash parameters, to force it adding the input to the output?

是:您可以使用-v命令行选项(“读取时打印shell输入行”)。你知道吗

相关问题 更多 >