Home: Perl Programming Help: Beginner:
simple ping script



ringo
New User

Sep 26, 2012, 3:00 AM


Views: 1947
simple ping script

Hi I am working on a simple cgi-script to print out ping results. It is a working script but with slight problem


#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use CGI qw(:standard);
use strict; # Check our syntax strictly
use diagnostics; # Tell us how to fix mistakes
use CGI; # Import the CGI module

print "Content-type: text/html\n\n";

open PING, 'ping -c 20 www.yahoo.com|' or die 'Failed';
while(<PING>) {
print "$_<br>";
}
close (PING);


--- This script prints the output only after the 20th ping. Is there a way I can print the result while the system is pinging real-time? This is because if the destination is not responding I would wait for sometime before I can find out that it is actually timing out. I would replicated this script as well for traceroute. Thanks in advance.


FishMonger
Veteran / Moderator

Sep 26, 2012, 7:13 AM


Views: 1941
Re: [ringo] simple ping script

It would be better to use the Net::Ping module instead of the piped open.
http://search.cpan.org/~smpeters/Net-Ping-2.36/lib/Net/Ping.pm

To answer your question, you need to disable output buffering, which is done by setting the $| var to a true value. i.e.,

Code
$| = 1;