Skip to content

Instantly share code, notes, and snippets.

@blabos-zz
Created July 10, 2010 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blabos-zz/470412 to your computer and use it in GitHub Desktop.
Save blabos-zz/470412 to your computer and use it in GitHub Desktop.
ip check using threads
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use Net::Ping;
my @hosts = map{'192.168.13.' . $_} (1 .. 254);
my $max_threads = scalar @hosts;
my @instances;
my @returns;
for (0 .. $max_threads - 1) {
$instances[$_] = threads->create(\&check, $hosts[$_]);
}
print 'All threads created.', $/;
for (0 .. $max_threads - 1) {
if (defined $instances[$_]) {
$returns[$_] = $instances[$_]->join || -1;
}
}
for (0 .. $max_threads - 1) {
print $hosts[$_], ': ', $returns[$_], $/;
}
print 'Normal Ending', $/;
##############################################################################
sub check {
my $ip = shift;
my $status = 0;
for (1 .. 3) {
my $np;
$np = Net::Ping->new('icmp');
$np->service_check(1);
last if $status = ($np->ping($ip, 1) || 0);
#last if $status = grep{/ 0% packet loss/} qx{/bin/ping -c 1 -w 1 $ip};
#last if $status = (system('/bin/ping', qw{-c 1 -w 1}, $ip) == 0);
$np = Net::Ping->new('tcp');
$np->service_check(1);
last if $status = ($np->ping($ip, 1) || 0);
$np = Net::Ping->new('udp');
$np->service_check(1);
last if $status = ($np->ping($ip, 1) || 0);
sleep 3;
}
return $status ? 'OK' : 'NOK';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment