473,410 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

Piping ping into perl-prog

Hello netties,

I coded the following in perl:

open(LOGFILE,">&STDOUT") ;

if ( $ARGV[0] ) {

close(LOGFILE) ;
open(LOGFILE,">> $ARGV[0]") or die "Cannot open file $ARGV[0]\n" ;
select(LOGFILE) ; # If commented out, nothing
$| = 1 ; # is printed to $ARGV[0] later on

}

while ( <STDIN> ) {

( $sec, $min, $hour, $day, $month, $year ) = localtime() ;
$month += 1 ;
$year += 1900 ;

printf LOGFILE ("%02d.%02d.%04d %02d:%02d:%02d: %s", $day, $month,
$year,
$hour, $min, $sec, $_) ;

}

The program is intended to receive input line by line from STDIN
(continuously, e.g. from UNIX-commands like ping or tail -f) and to
print the input line prepended with a timestamp either to STDOUT or to
a file if a file name is given on the command line.

If I pipe an "ordinary" file to this program:

$ cat test_inp.txt | tstamp.pl

I get all lines of the file printed to the terminal or to a file:

$ cat test_inp.txt | tstamp.pl test_out.txt

each line prepended with the time stamp.

If I try the same with the select-function and the setting of
unbuffered output commented out, i.e.,

close(LOGFILE) ;
open(LOGFILE,">> $ARGV[0]") or die "Cannot open file $ARGV[0]\n" ;
# select(LOGFILE) ; # If commented out, nothing
# $| = 1 ; # is printed to $ARGV[0] later on

and redirect from a continuously writing program:

ping <host> | tstamp.pl

I get proper output on the terminal, but a file as the output target
remains empty. If instead unbuffered output is used ($| = 1), then
output to the file works fine as well.

Does somebody know the reason for this ?

Any help is appreciated.

Cheers
Bernd
Jul 19 '05 #1
3 7758
bernd wegener wrote:
ping <host> | tstamp.pl

I get proper output on the terminal, but a file as the output target
remains empty. If instead unbuffered output is used ($| = 1), then
output to the file works fine as well.
You mean "the output target file remains empty until perl's internal
buffers fill up and perl writes several K bytes all at once".
Try letting the program run for 24 hours and see what happens.
Does somebody know the reason for this ?


That behavior is exactly what $| was created to control.
The stdio routines operate differently based on whether the C library
routine isatty() returns 1 or 0, and perl does so as well.
Output to non tty file handles gets buffered.
-Joe
Jul 19 '05 #2
Joe Smith <Jo*******@inwap.com> wrote in message news:<5W02d.306624$8_6.235832@attbi_s04>...
bernd wegener wrote:
ping <host> | tstamp.pl

I get proper output on the terminal, but a file as the output target
remains empty. If instead unbuffered output is used ($| = 1), then
output to the file works fine as well.


You mean "the output target file remains empty until perl's internal
buffers fill up and perl writes several K bytes all at once".
Try letting the program run for 24 hours and see what happens.
Does somebody know the reason for this ?


That behavior is exactly what $| was created to control.
The stdio routines operate differently based on whether the C library
routine isatty() returns 1 or 0, and perl does so as well.
Output to non tty file handles gets buffered.
-Joe


Hi Joe,

thanks, that was a short but instructive lesson in I/O. If one knows
it is quite straightforward to understand, but since I was not patient
enough I did not see "the end", i.e. that eventually the data is
written to the file. I was not aware of the fact that the
stdio-library differentiates between terminals and disk files in that
way.

I made a little "experiment" on my UNIX-Box, which turned out that the
first chunk of data written to the file was 8K of size (after a few
minutes, so it was not necessary to wait 24 h staring at the terminal
:->>).

I took the Perl Cookbook, reading a little bit more about buffered /
non-buffered output ... ;-)

Thanks and Bye.

Bernd
Jul 19 '05 #3
In article <18**************************@posting.google.com >, bernd
wegener <be****@gmx.net> wrote:
Hello netties,

I coded the following in perl:

open(LOGFILE,">&STDOUT") ;

if ( $ARGV[0] ) {

close(LOGFILE) ;
open(LOGFILE,">> $ARGV[0]") or die "Cannot open file $ARGV[0]\n" ;
select(LOGFILE) ; # If commented out, nothing
$| = 1 ; # is printed to $ARGV[0] later on

}

while ( <STDIN> ) {

( $sec, $min, $hour, $day, $month, $year ) = localtime() ;
$month += 1 ;
$year += 1900 ;

printf LOGFILE ("%02d.%02d.%04d %02d:%02d:%02d: %s", $day, $month,
$year,
$hour, $min, $sec, $_) ;

}

The program is intended to receive input line by line from STDIN
(continuously, e.g. from UNIX-commands like ping or tail -f) and to
print the input line prepended with a timestamp either to STDOUT or to
a file if a file name is given on the command line.

If I pipe an "ordinary" file to this program:

$ cat test_inp.txt | tstamp.pl

I get all lines of the file printed to the terminal or to a file:

$ cat test_inp.txt | tstamp.pl test_out.txt

each line prepended with the time stamp.

If I try the same with the select-function and the setting of
unbuffered output commented out, i.e.,

close(LOGFILE) ;
open(LOGFILE,">> $ARGV[0]") or die "Cannot open file $ARGV[0]\n" ;
# select(LOGFILE) ; # If commented out, nothing
# $| = 1 ; # is printed to $ARGV[0] later on

and redirect from a continuously writing program:

ping <host> | tstamp.pl

I get proper output on the terminal, but a file as the output target
remains empty. If instead unbuffered output is used ($| = 1), then
output to the file works fine as well.
What do you mean by "proper output on the terminal". When I run this
command, I get no output on the terminal, but I do get output to the
file. Of course, I have to wait awhile because the output is buffered
and it takes a minute for the ping command to fill up a buffer-full.
You shouldn't be getting output from the ping command on your terminal
because you are only writing it to a file.

Does somebody know the reason for this ?


I cannot reproduce your results.

FYI: this newsgroup is defunct; try comp.lang.perl.misc in the future.
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Alexandre Plennevaux | last post by:
hello ! I would like to measure the time a user needs to connect to my website. In other words, to perform a ping between my website and the user's computer. I'm scratching my head as to how...
0
by: Sleiman | last post by:
Hello newsgroup readers I am looking for a php implementation for xmlrpc ping server. Do you have any idea where I can find one? Using mod_pubsub I can have a very nice blogchatter style of...
1
by: john prokopek | last post by:
I just installed ActiveState Perl 5.8 on a Win2k system. For a sanity check I a one line script to take input from STDIN and print. Hey it doesn't work. If I just perform a print with some text...
1
by: erock34 | last post by:
Trying to ping a list of servers using ping, but I get a return on the unpingable servers. I want to get a list of unsuccessful pings only!! here is a sample... #!/usr/bin/perl use Net::Ping; ...
7
by: Igna | last post by:
Hello. I have to write a pipe to joint a GUI in perl and a simulation program in c. I have read all the docs found in perl.com and now I am trying to make a test with this simple program. It seems...
12
by: Matt F | last post by:
I'm creating simple ping script that prompts a user for various parameters. However, I would like each line of the ping displayed as it is received. Currently, the result isn't displayed until...
7
by: patelxxx | last post by:
Guy's, On my homepage website I want to display the current status of our server. The idea I have is to ping (or send my ip address) to our server every 5 mins and the result I get back should...
3
by: patelxxx | last post by:
PERL Code to Ping a server: #!c:/Perl/bin/perl.exe use Net::Ping; use strict; use warnings; my $host = 192.168.0.1; # Real value removed by MODERATOR
3
by: imughal | last post by:
I got the perl script which does following task. This solution reads in a line from a text file that is passed in as input parameter 1 to a Perl script. This script will then ping the machine...
6
by: Dave Marden | last post by:
I currently use this routine in vbscript to ping computers and get the status of ping to determine whether to try to backup a machine, I am trying to change it to work with vb2003.net I am...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.