How to read buffered output from ping-like process

Discussion in 'Perl Misc' started by Chris, Mar 16, 2005.

  1. Chris

    Chris Guest

    I am trying to read some data from another process, let’s call the
    program foo (binary and I don’t have the source). It works like ping in
    that it spits output to the screen every few seconds and has to be
    killed with control-c. I can write a script to read data from ping and
    kill the ping after say 10 seconds. But I don’t get any data from foo
    even though it behaves just like ping at the command prompt. (I even
    tried sending the data to a file but data doesn’t show up until 4096
    bytes are written and that is too long to wait.) I’ve tried all kinds of
    things like setting the file desc to non-blocking, using sysread, and
    even fork (open FD, “-|”) with foo as the child, but still no luck. Any
    ideas are appreciated. What is the shell doing to get the buffered
    output from foo on a line-by-line basis? Here is what I have that works
    for ping (on linux).

    Thanks, Chris

    eval
    {
    local $SIG{'ALRM'} = sub { die "alarm\n"; };
    alarm 10;
    unless(open(FD, "/bin/ping myhost |"))
    {
    die "Unable to open pipe\n";
    }
    while($line = <FD>)
    {
    print "$line";
    }
    unless(close(FD))
    {
    die "Unable to close pipe\n";
    }
    alarm 0;
    };
    local $SIG{'INT'} = 'IGNORE';
    kill INT => -$$;
    Chris, Mar 16, 2005
    #1
    1. Advertising

  2. > eval
    > {
    > local $SIG{'ALRM'} = sub { die "alarm\n"; };
    > alarm 10;
    > unless(open(FD, "/bin/ping myhost |"))
    > {
    > die "Unable to open pipe\n";
    > }
    > while($line = <FD>)
    > {
    > print "$line";
    > }
    > unless(close(FD))
    > {
    > die "Unable to close pipe\n";
    > }
    > alarm 0;
    > };
    > local $SIG{'INT'} = 'IGNORE';
    > kill INT => -$$;


    Chris,
    This code printed the ping results on STDOUT, it never went to the file handle.
    If that is the case for you, just redirect the STDOUT to a file or var
    temporarily.
    Chad Columbus, Mar 16, 2005
    #2
    1. Advertising

  3. Chris

    Guest

    Chris <> wrote:
    > I am trying to read some data from another process, let’s call the
    > program foo (binary and I don’t have the source). It works like ping in
    > that it spits output to the screen every few seconds and has to be
    > killed with control-c. I can write a script to read data from ping and
    > kill the ping after say 10 seconds. But I don’t get any data from foo
    > even though it behaves just like ping at the command prompt. (I even
    > tried sending the data to a file but data doesn’t show up until 4096
    > bytes are written and that is too long to wait.)


    My guess:

    ping always uses unbuffered (or line buffered) output.

    foo is trying to be clever, and it tries to detect if its output is
    being sent to a tty. If it is sent to a tty, it uses unbuffered output.
    If not, it uses buffered output.

    If you can't fix foo, you have to trick it into thinking the pipe it is
    hooked to is a tty. Sadly, I don't know how to do this off the top of my
    head.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Mar 16, 2005
    #3
  4. wrote:

    > If you can't fix foo, you have to trick it into thinking the pipe it is
    > hooked to is a tty. Sadly, I don't know how to do this off the top of my
    > head.


    I >>>Expect<<< there's something on CPAN.
    Brian McCauley, Mar 16, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    2,773
  2. Replies:
    9
    Views:
    664
    Michael Wojcik
    Aug 23, 2005
  3. reid
    Replies:
    4
    Views:
    659
  4. Genstein
    Replies:
    7
    Views:
    679
    Genstein
    May 12, 2011
  5. Jeff Wood
    Replies:
    1
    Views:
    105
    aurelianito
    Oct 22, 2005
Loading...

Share This Page