Catching a direct redirect to /dev/tty

假如想象 提交于 2020-01-11 03:05:10

问题


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?


回答1:


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).




回答2:


 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]




回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/307006/catching-a-direct-redirect-to-dev-tty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!