捕获到 /dev/tty 的直接重定向

发布于 2024-07-09 16:25:41 字数 307 浏览 3 评论 0原文

我正在为一个直接向 /dev/tty 吐出文本的程序开发一个应用程序控制器。

这是一个生产应用程序控制器,必须能够捕获所有发送到终端的文本。 一般来说,这不是问题。 我们只需重定向 stdout 和 stderr 即可。 这个特定的应用程序直接调用 echo 并将结果重定向到 /dev/tty (echo "some text" > /dev/tty)。 通过我的应用程序控制器进行重定向无法捕获文本。

我确实有这个应用程序的源代码,但我无法修改它,也不再维护它。 关于如何捕获和/或丢弃输出有什么想法吗?

I'm working on an application controller for a program that is spitting text directly to /dev/tty.

This is a production application controller that must be able to catch all text going to the terminal. Generally, this isn't a problem. We simply redirect stdout and stderr. This particular application is making direct calls to echo and redirecting the result to /dev/tty (echo "some text" > /dev/tty). Redirects via my application controller are failing to catch the text.

I do have the source for this application, but am not in a position to modify it, nor is it being maintained anymore. Any ideas on how to catch and/or throw away the output?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

墨离汐 2024-07-16 16:25:42

这就是我在 python 中所做的

import pty, os

pid, fd = pty.fork()
if pid == 0: # In the child process execute another command
    os.execv('./my-progr', [''])
    print "Execv never returns :-)"
else:
    while True:
        try:
            print os.read(fd,65536),
        except OSError:
            break

This is what I did in python

import pty, os

pid, fd = pty.fork()
if pid == 0: # In the child process execute another command
    os.execv('./my-progr', [''])
    print "Execv never returns :-)"
else:
    while True:
        try:
            print os.read(fd,65536),
        except OSError:
            break
走野 2024-07-16 16:25:42

我无法完全确定 @flolo 提到的 screen 程序是否能满足您的需要。 可能会,但我不确定是否有内置的日志记录工具,这似乎正是您所需要的。

可能已经有一个程序可以满足您的需要。 我提名 sudosh 作为一种可能性。

如果您最终需要编写自己的,您可能需要使用伪 tty (pty) 并将应用程序控制器置于用户的真实终端连接和 pty 设备之间,在那里它可以记录您需要的任何内容它要记录。 这不是小事。 您可以在 Rochkind 的“高级 UNIX 编程,第二版”中找到相关信息书,毫无疑问还有其他类似的书(Stevens 的“UNIX 环境中的高级编程”书是可能的候选书,但我没有副本来验证这一点)。

I can't quite determine whether the screen program mentioned by @flolo will do what you need or not. It may, but I'm not sure whether there is a logging facility built in, which appears to be what you need.

There probably is a program out there already to do what you need. I'd nominate sudosh as a possibility.

If you end up needing to write your own, you'll probably need to use a pseudo-tty (pty) and have your application controller sit in between the user's real terminal connection and the the pty device, where it can log whatever you need it to log. That's not trivial. You can find information about this in Rochkind's "Advanced UNIX Programming, 2nd Edn" book, and no doubt other similar books (Stevens' "Advanced Programming in the UNIX Environment" book is a likely candidate, but I don't have a copy to verify that).

讽刺将军 2024-07-16 16:25:41
 screen -D -m yourEvilProgram

应该管用。 自从我使用它以来已经过去了很长时间,但是如果您需要读取它的一些输出,您甚至可以利用一些套接字来读取它。

[添加:两个链接, RackaidPixelbeat,以及主页 GNU]

 screen -D -m yourEvilProgram

should work. Much time passed sinced I used it, but if you need to read some of its output it could even be possible that you could utilize some sockets to read it.

[Added: two links, Rackaid and Pixelbeat, and the home page at GNU]

套路撩心 2024-07-16 16:25:41

控制此类应用程序的经典解决方案是 Expect,它设置伪终端,进行日志记录,并从脚本驱动受控应用程序。 它附带了大量示例脚本,因此您可能只需调整一个脚本即可满足您的需求。

The classic solution to controlling an application like this is Expect, which sets up pseudo-terminals, does logging, and drives the controlled application from a script. It comes with lots of sample scripts so you can probably just adapt one to fit your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文