Linux:捕获已运行进程的输出(用纯 C 语言!)
我的情况如下:我有很多小玩意(非常接近路由器,不完全是,但无论如何这是不相关的);他们正在运行一个基于 MIPS 的简单 Linux 发行版。
要控制它们,可以通过远程登录(通过串行端口)并向交互式类似 bash 的 shell 发出命令,然后该 shell 写回一些输出。 shell 的输入和输出都附加到/dev/ttyAS0。
现在,我想自动化所有这一切,即编写一个程序,该程序将在小发明内运行,成为侦听某个端口的小型服务器,并将任何命令传递给所述 shell,捕获 shell 的输出并中继它返回给联系服务器的人。
I:
1) 可以在 gizmo 内安装(小,<500KB)程序 2) 无法修改操作系统、启动脚本、shell 等任何内容 3)具有root权限 4)知道如何编写SOAP服务器 5) 知道如何获取 SOAP 消息,将其转换为命令并将其注入到 /dev/ttyAS0 6) 不知道如何捕获 shell 的回复 7) 知道如何在获得 shell 的回复后将其转换回 SOAP 消息并回复原始询问者。
所以基本上,问题是 6):如何将字符串注入到 /dev/ttyAS0 并让 shell 执行它,捕获 shell 的输出?
我知道
http:// etbe.coker.com.au/2008/02/27/redirecting-output-from-a-running-process/
即我知道如果我在里面运行 GDB (或 strace ),我可以更改 shell 的标准输出盒子,但我无法将其安装在那里 - 它太大了,而且无论如何,这种方法看起来太像黑客了。
所以,总结一下:
root 如何在纯 C 语言中捕获已经运行的进程的 stdout,无需 gdb 或 strace,并且无法访问进程启动的方式?
或者 - 几乎等效 - 如何捕获正在写入终端的内容,在 PURE C 中?
My situation is the following: I've got a lot of small gizmos ( pretty close to routers, not exactly but anyway that's irrelevant) ; they are running a bare-bones MIPS-based Linux distro.
To control them, one can telnet there ( thru serial port ) and issue commands to an interactive bash-like shell which then writes back some output. The shell's input and output are both attached to /dev/ttyAS0.
Now, I'd like to automate all of this, i.e. write a program that will run inside the gizmo, be a small server listening on some port, and which would pass on any command to the said shell, capture shell's output and relay it back to whoever contacted to server.
I:
1) can install (small, <500KB) programs inside the gizmo
2) can't modify the OS, startup scripts, the shell, anything
3) have root access
4) know how to write a SOAP server
5) know how to get a SOAP message, translate it to a command and inject it into /dev/ttyAS0
6) DONT KNOW how to capture the shell's reply
7) know how to, having shell's reply, translate it back to a SOAP message and reply to the original inquirer.
So basically, the problem is 6) : how to, having injected a string to /dev/ttyAS0 and thus having made the shell execute it, capture the shell's output ?
I am aware of
http://etbe.coker.com.au/2008/02/27/redirecting-output-from-a-running-process/
i.e. I know that I could change the shell's stdout if I had GDB ( or strace ) running inside the box, but I can't install it there - it's too big and anyway this approach seems too much like a hack.
So, summarizing:
How root can capture stdout of an already running process, IN PURE C, without gdb or strace, with no access to the way the process is started?
Or - almost equivalently - how to capture what's being written to a terminal, IN PURE C ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想看看
You might want to take a look at reptyr. It will probably need some adaptation to work for your system though
您是否尝试过使用 kermit 脚本驱动串行端口?我可能会放弃尝试在设备上插入更聪明的代理,而只是尝试驱动现有的接口。
如果您确实想在设备上获取它,您可以查看类似 screen 的源代码 或 kermit 了解它们如何与 tty 交互。
Have you tried driving the serial port with a kermit script? I would probably forgo trying to insert a more clever proxy on the device and just try and drive the existing interface.
If you really want to get it on the device, you may be able to look at the source to something like screen or kermit to get a sense of how they interact with ttys.