Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

trapping output from /dev/tty

by former33t (Scribe)
on Oct 04, 2006 at 14:29 UTC ( #576335=perlquestion: print w/replies, xml ) Need Help??
former33t has asked for the wisdom of the Perl Monks concerning the following question:

I am working with a binary on a Linux system I don't have the source to that dumps data from an in-memory database. I tried opening the process with the pipe and reading the output, but this doesn't work for me. I get the administrative messages, but not the output. Running strace on the program shows that the program writes all its administrative messages (connecting, disconnecting, etc) to STDOUT, but then inexplicably opens /dev/tty directly and writes all the data (what I really wanted) to there. Does anyone know how I could write a pseudo terminal 'wrapper' program to trap this output? I did some searches to see if someone has already had this problem, but didn't see anything. BTW, I'm not above performing shell redirection if anyone knows how to trap output sent directly to /dev/tty, but I don't think this can be done. Thanks in advance for any help.

Replies are listed 'Best First'.
Re: trapping output from /dev/tty
by shmem (Chancellor) on Oct 04, 2006 at 16:18 UTC
    warning - very ugly hack

    If you are absolutely alone on that system - here's a very very bad, ugly, naughty hack someone (I won't reveal who) uses in administration shell scripts(!) to set passwords for databases etc.: he just moves away /dev/tty, writes the password into /dev/tty (textfile!) twice and invokes 'passwd someuser'. Hell breaks loose if somebody in the meantime opens a pager. This is really a bad daily wtf. I never did such a thing, honest!

    So if you just want the output - save your /dev/tty ... well, I trust you're reasonable! ... don't forget to restore /dev/tty

    Shame on me for writing this *blush*


    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: trapping output from /dev/tty
by Fletch (Chancellor) on Oct 04, 2006 at 14:34 UTC

    You can use the Expect module to run your program on a PTY (or perhaps IPC::Run or IO::Pty if you don't really need Expect's send/expect chat-y functionality).

Re: trapping output from /dev/tty
by RMGir (Prior) on Oct 04, 2006 at 16:53 UTC

    Sounds unfun.

    The pty route seems like the way to go. But if you do need to go with anything strange (like taking over /dev/tty), consider running the program chrooted, so you can at least have /dev/tty be a local entity, not affecting the rest of the system...

    A better option is likely to use LD_PRELOAD to load a different implementation of open, which returns an fd to a normal file if someone tries to open /dev/tty for output.

    For example, see this, and adapt that example for open rather than time. Sorry, I don't know offhand where open is actually defined, although seems like a reasonable guess.


    Edit: updated link

Re: trapping output from /dev/tty
by Anonymous Monk on Oct 04, 2006 at 22:22 UTC

    if you are on linux, you probably have the expect command bundled. there are many examples that come with the expect distribution. and

    maybe you can adapt one of these.

      i forgot to mention in the previous post that the output of 'man 7 tty' o 'man 4 tty' is of interest

      **HP-UX 11.0**: The file /dev/tty is, in each process, a synonym for the control terminal associated with the process group of that process, if any. It is useful for programs or shell sequences that need to be sure of writing messages on the terminal no matter how output has been redirected. It can also be used for programs that demand the name of a file for output, when typed output is desired and it is tiresome to find out what terminal is currently in use.

      --steph (sgt)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://576335]
Approved by chargrill
Front-paged by diotalevi
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2018-06-25 00:30 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.