my @lines = `ping $self->{broadcast_ip} -b -c2 2>&1 | grep \'bytes from\'`;
####
#!/usr/bin/perl -w
use strict;
use Net::Ping;
use Time::HiRes qw(usleep);
use Fcntl qw(:DEFAULT :flock);
use POSIX qw(tmpnam mkfifo);
my $master = $$;
# Create the FIFO
my $fifo;
do {
$fifo = tmpnam();
} until mkfifo($fifo, 0666);
# Generate list of hosts to ping
my @targets;
foreach my $target (84..87) {
push(@targets, '10.100.19.'.$target);
}
# Make the pinger
my $pinger = Net::Ping->new('icmp', 5);
# Fork off processes
my %kids;
foreach my $target (@targets) {
my $kid = fork;
if($kid) {
# Parent
$kids{$kid} = $target;
}
else {
# Child
print "Child $target started\n";
if($pinger->ping($target)) {
# PING! Throw it in the FIFO
print "Found $target!\n";
sysopen(FH, $fifo, O_WRONLY | O_APPEND)
or die "Can't open FIFO $fifo: $!\n";
print "Locking FIFO\n";
flock(FH, LOCK_EX)
or die "Can't lock FIFO $fifo: $!\n";
print "appending to FIFO\n";
print FH $target."\n";
close(FH);
}
else {
warn "$target: $!\n";
print "No response from $target\n";
}
$pinger->close();
print "Child $target exiting\n";
exit();
}
# Sleep 25 ms to prevent flooding
usleep(25_000);
}
# Cleanup processes and gather results
my @pings;
print "Parent opening FIFO\n";
sysopen(FIFO, $fifo, O_RDONLY | O_NONBLOCK)
or die "Can't open FIFO $fifo for reading: $!\n";
print "Parent looping over remaining kids\n";
while(%kids) {
print "Parent looping wait\n";
while((my $kid = wait()) > 0) {
print "Parent reaping $kids{$kid}\n";
delete($kids{$kid});
print "Parent reading fifo\n";
while(defined(my $line = )) {
chomp($line);
print "Parent got $line!\n";
push(@pings, $line);
}
}
}
print "Parent closing fifo\n";
close(FIFO);
foreach my $ping (@pings) {
print "PONG: $ping\n";
}
# Delete the FIFO whenever we exit
END {
if($$ == $master) {
unlink($fifo)
or die "Couldn't unlink FIFO $fifo: $!\n";
print "$$ unlinked $fifo\n";
}
}
####
09:33:29.275634 arp who-has 10.100.19.84 tell 10.100.19.1
09:33:29.305634 10.100.19.1 > 10.100.19.85: icmp: echo request (DF)
09:33:29.305634 10.100.19.85 > 10.100.19.1: icmp: echo reply
09:33:29.355634 10.100.19.1 > 10.100.19.86: icmp: echo request (DF)
09:33:29.355634 10.100.19.86 > 10.100.19.1: icmp: echo reply
09:33:29.395634 arp who-has 10.100.19.87 tell 10.100.19.1
09:33:30.275634 arp who-has 10.100.19.84 tell 10.100.19.1
09:33:30.395634 arp who-has 10.100.19.87 tell 10.100.19.1
09:33:31.275634 arp who-has 10.100.19.84 tell 10.100.19.1
09:33:31.395634 arp who-has 10.100.19.87 tell 10.100.19.1
####
script output:
Child 10.100.19.84 started
10.100.19.84:
No response from 10.100.19.84
Child 10.100.19.84 exiting
Child 10.100.19.85 started
Found 10.100.19.85!
Child 10.100.19.86 started
Found 10.100.19.86!
Child 10.100.19.87 started
10.100.19.87:
No response from 10.100.19.87
Child 10.100.19.87 exiting
Parent opening FIFO
Parent looping over remaining kids
Parent looping wait
Parent reaping 10.100.19.87
Parent reading fifo
Parent reaping 10.100.19.84
Parent reading fifo
Locking FIFO
appending to FIFO
Child 10.100.19.85 exiting
Parent reaping 10.100.19.85
Parent reading fifo
Parent got 10.100.19.85!
Locking FIFO
appending to FIFO
Child 10.100.19.86 exiting
Parent reaping 10.100.19.86
Parent reading fifo
Parent got 10.100.19.86!
Parent closing fifo
PONG: 10.100.19.85
PONG: 10.100.19.86
28674 unlinked /tmp/fileMVZqps
tcpdump:
10:27:38.255634 arp who-has 10.100.19.84 tell 10.100.19.1
10:27:39.255634 arp who-has 10.100.19.84 tell 10.100.19.1
10:27:40.255634 arp who-has 10.100.19.84 tell 10.100.19.1
10:27:48.255634 10.100.19.1 > 10.100.19.85: icmp: echo request (DF)
10:27:48.255634 10.100.19.85 > 10.100.19.1: icmp: echo reply
10:27:51.665634 arp who-has 10.100.19.86 tell 10.100.19.1
10:27:51.665634 arp reply 10.100.19.86 is-at 0:0:50:b:b3:7f
10:27:53.255634 arp who-has 10.100.19.85 tell 10.100.19.1
10:27:53.255634 arp reply 10.100.19.85 is-at 0:0:50:b:b3:7f
10:27:58.275634 10.100.19.1 > 10.100.19.86: icmp: echo request (DF)
10:27:58.275634 10.100.19.86 > 10.100.19.1: icmp: echo reply
10:28:02.375634 arp who-has 10.100.19.1 tell 10.100.19.85
10:28:02.375634 arp reply 10.100.19.1 is-at 0:7:e9:9:8a:dd
10:28:08.295634 arp who-has 10.100.19.87 tell 10.100.19.1
10:28:09.295634 arp who-has 10.100.19.87 tell 10.100.19.1
10:28:10.295634 arp who-has 10.100.19.87 tell 10.100.19.1