Re: problema ping e perl [SOLVED]

2006-05-02 Per discussione Fabio
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?

Fabio

On Tuesday 02 May 2006 18:01, Fabio wrote:
 ciao a tutti,
 ho uno strano problema che non riesco a risolvere, spero in un aiuto della
 lista.

 il sistema è debian sarge e kernel 2.6.8.

 devo fare uno script (che viene eseguito ogni ora) che fa il ping su certi
 ip e scrive dei file di log. ho scritto lo script in perl (poichè ho
 modificato uno script già esistente).

 nello script eseguo:
$state=system ping -w 5 -c 5 217.xx.yy.zz ;
 e poi controllo se $state è 0. (da notare -w: dopo 5 secondi il ping
 dovrebbe terminare).

 il problema è:
 - se eseguo lo script direttamente sulla shell il ping termina anche se non
 ottiene le risposte
 - se eseguo lo script da Zope e mediante sudo, il ping non termina

 ho provato a mettere degli export VAR=value per ogni variabile visualizzata
 da env ma senza risultato. Non so dove sbattere la testa poichè a questo
 punto non so dove possa stare il problema: ho verificato che in entrambi i
 casi è l'utente root che esegue lo script, ho messo le variabili
 d'ambiente... non so più che fare!

 qualcuno mi può indicare la via?

 Grazie 1000,
 Fabio

-- 

Dott. Fabio Marcone

2T srl
Telefono   +39 - 0871- 540154
Fax+39 - 0871- 571594
Email  [EMAIL PROTECTED]
Indirizzo  Viale B. Croce 573
   66013 Chieti Scalo (CH)
GNU/Linux registered user  #400424



Re: problema ping e perl [SOLVED]

2006-05-02 Per discussione Mattia Dongili
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 = ($a24) + ($b16) + ($c8) + ($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  0xff00)  24,
($ip  0x00ff)  16,
($ip  0xff00)   8,
 $ip  0x00ff);
}

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!