How to read buffered output from ping-like process

C

Chris

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 => -$$;
 
C

Chad Columbus

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

xhoster

Chris said:
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
 
B

Brian McCauley

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top