[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: problema ping e perl [SOLVED]



On Tue, May 02, 2006 at 06:26:40PM +0200, Fabio wrote:
> ciao,
> ho risolto utilizzando la libreria Net::Ping ma rimane la curiosità di capire 
> come mai con la chiamata del comando shell si aveva quel comportamento 
> strano, qualcuno può azzardare ipotesi?

no... pero' ti riciclo questo script (che era stato richiesto su altre
ML):

#! /usr/bin/perl -w

use strict;
use Net::Ping;
use threads;
use threads::shared;

my @hosts : shared;
my @hosts_alive : shared ;
my @running_threads;

my $NUMBER_THREADS;
my ($ipstring, $temp, $i);

if ($#ARGV < 0) {
	die "usa $0 aaa.bbb.ccc.ddd/mask\n";
}

my ($hexip, $mask) = split /\//, shift;
my ($a, $b, $c, $d) = split /\./, $hexip;

$mask = ($mask && $mask < 32 ? ~0x0 >> $mask : 0);
$hexip = ($a<<24) + ($b<<16) + ($c<<8) + ($d);
print (sprintf("0x%0.8x (%s) - 0x%0.8x (%s)\n", $hexip, ip2string($hexip), ~$mask, ip2string(~$mask)));

$hexip &= ~$mask if ($mask);
$i = 0;
do {{
	$temp = $hexip + $i;
	# salta x.y.z.0
	next unless ($temp & 0xff);
	# salta x.y.z.255
	next if (($temp & 0xff) == 0xff);
	$ipstring = ip2string($temp);
	print (sprintf("0x%0.8x - %s\n", $temp, $ipstring));
	push @hosts, $ipstring;

}} while (++$i & ($mask));

# launch the threads
$NUMBER_THREADS = @hosts > 10 ? 10 : @hosts;
print "Creating $NUMBER_THREADS threads.\n";
for (1 .. $NUMBER_THREADS){
	push @running_threads, threads->create( \&scan, $_ );
}

# now, wait for them to complete their work.
while (@running_threads){
	$running_threads[0]->join;
	shift @running_threads;
}

print join ("\n", @hosts_alive);

exit(0);

sub ip2string {
	my $ip = shift;
	return sprintf("%d.%d.%d.%d",
			($ip & 0xff000000) >> 24,
			($ip & 0x00ff0000) >> 16,
			($ip & 0x0000ff00) >>  8,
			 $ip & 0x000000ff);
}

sub scan {

	my $current_host;
	my $me = shift || threads->tid();

	while (1){

		# try to get a first host to test
		LOCK_INIT_HOST: {
			lock( @hosts );
			$current_host = shift @hosts;
		}
		last unless defined($current_host);

		#print "[$me] provo host : $current_host\n";
		# we have a server to test:
		my $p = Net::Ping->new();

		if ($p->ping($current_host)){
			$p->close();
			print "[$me] $current_host is alive!\n";
			# we store the result
			LOCK_DONE: {
				lock( @hosts_alive );
				push @hosts_alive, $current_host;
			}
		} else {
			print "[$me] $current_host is dead.\n";
		}
	}
}

-- 
mattia
:wq!



Reply to: