chimni has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use strict; use warnings; my $timestamp=(`date -u +%Y-%m-%d,%T`); chomp($timestamp); print "$timestamp"; open(FI,">forkdata") || die ("Could not open file:$!"); sub ping_a_host { my $host = shift; @ping_output=`ping $host 16 -n 4 2>/dev/null`; if (grep { /round-trip\s+\(ms\)/ ; if ($_ =~ m#min/avg/max\s+=\s ++(\d+)/(\d+)/(\d+)#) {print FI "$host,$timestamp,$1,$2,$3\n"}} @ping +_output){ return 1;} elsif (grep {/0 packets rec/} @ping_output){ print FI "$host,$timestamp,NULL,NULL,NULL\n"; return 0; } } my %pid_to_host; sub wait_for_a_kid { my $pid = wait; return 0 if $pid < 0; my $host = delete $pid_to_host{$pid} or warn("Why did I see $pid ($?)\n"), next; warn "reaping $pid for $host\n"; 1; } my @hosts = map "192.198.192.$_", "221".."251"; for (@hosts) { wait_for_a_kid() if keys %pid_to_host > 10; if (my $pid = fork) { ## parent does... $pid_to_host{$pid} = $_; warn "$pid is processing $_\n"; } else { # child does ## child does... exit !ping_a_host($_); } } ## final reap: 1 while wait_for_a_kid(); close FI;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Ping multiple hosts (fork implementation)
by Juerd (Abbot) on Mar 09, 2004 at 16:41 UTC | |
by rob_au (Abbot) on Mar 09, 2004 at 23:00 UTC | |
Re: Ping multiple hosts (fork implementation)
by UnderMine (Friar) on Mar 09, 2004 at 16:27 UTC | |
Re: Ping multiple hosts (fork implementation)
by NetWallah (Canon) on Mar 09, 2004 at 17:03 UTC |