I'm having a problem with the script below. It runs through all my addresses
fine but when it gets to the last one in the array, the program just hangs
there. My output file is empty so it hasn't been closed yet but if I hit
CTRL-C, the file is closed and the script stops.  I assume I need to somehow
say my array of addresses is done but how would I do that below?  Any help
would be greatly appreciated.

#!/usr/bin/perl -w

BEGIN {
  die "POE::Component::Client::Ping requires root privilege\n"
  if $> and ( $^O ne 'VMS' );
}

use POE;
use POE::Component::Client::Ping;

# How many seconds to wait for ping responses.
sub PING_TIMEOUT () { 5 }

my $outfile = "/logs/tpaping3.log";

my @addresses = qw(1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4);

POE::Component::Client::Ping->spawn
 ( Alias => 'pinger',
   Timeout => PING_TIMEOUT,
  OneReply => 1
 );

POE::Session->create
 ( inline_states =>
   { _start => \&client_start,
     pong => \&client_got_pong,
   }
 );

open (OUT_FH, "> $outfile");
print OUT_FH "Started ".scalar(localtime)."\n";
$poe_kernel->run();
close (OUT_FH);
exit;

sub client_start {
  my ( $kernel, $session ) = @_[ KERNEL, SESSION ];
  print "Starting to ping hosts.\n";
  for (1..20) {
  my $address = shift @addresses;
  last unless defined $address;
    print "Pinging $address at ", scalar(localtime), "\n";
    $kernel->post( pinger => ping => pong => $address );
  }
}

sub client_got_pong {
  my ( $kernel, $session ) = @_[ KERNEL, SESSION ];
  my $request_packet = $_[ARG0];
  my ( $request_address, $request_timeout, $request_time ) =
@{$request_packet};
  my $response_packet = $_[ARG1];
  my ( $response_address, $roundtrip_time, $reply_time ) =
@{$response_packet};
  if (! defined $response_address ) {
    print OUT_FH "Timed out waiting for responses from $request_address.\n";
  }
 my $address = shift @addresses;
 if (defined $address) {
    print "Pinging $address at ".scalar(localtime)."\n";
   $kernel->post( pinger => ping => pong => $address );
 }
}


Reply via email to