473,574 Members | 2,981 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.%04 d %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 7767
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*******@inwa p.com> wrote in message news:<5W02d.306 624$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.%04 d %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
5658
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 i can do that. Does anyone know a good way to make this happen? thanks a lot,
0
1664
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 xmlrpc ping server, but it is in perl and my client's system admin has moved away from perl. What my client wants: a real-time listing of blogs...
1
1766
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 that works. So can someone clue me in. So, why doesn't this print out anything: while(<STDIN>) { print;}
1
1810
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; my $p = Net::Ping->new(`tcp`); open (TT, "box3");
7
1825
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 working but it does not write "bonjour" at the end. Can anybody tell me where my mistake is? I will be happy for any sample of code for a pipe...
12
7358
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 the ping is finished. <?php $ping_ip_addr = $_GET;
7
1951
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 result (and display on my homepage) in a success or failure of the server. Can someone please help me with the coding for this or just start me off?...
3
1548
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
8360
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 using TCP pings to the remote hosts echo port and will return up/down and the ping response time. A second optional attribute may be passed in that will...
6
3450
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 wondering if anyone has a ping function they could share with me. I have done some searching on this but cannot find anything specifically for vb2003. ...
0
7762
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8105
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8272
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7859
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5657
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3774
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1101
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.