Hello eveybody,

I am currently writing a script to check if a DHCP server is working. If it's working, i have to give the duration of the request. If not, an error's message.

To do this test, i am using the module NET: DHCP::WATCH (http://search.cpan.org/~ejdrs/Net-DH...-2.03/Watch.pm ) et le module Object: Destroyer.

I have first created this small script to test my alarm/timeout.

Code:
use Object::Destroyer;
 
eval {
 
    local $SIG{ALRM} = sub { die("alarm\n") };
 
    alarm(4);
	my $sentry = Object::Destroyer->new( sub {alarm(0)} );
 
	#Get Status
    sleep (2)
 
};
 if ($@) {print "Alarm";}
else {
   print "OK";}
I have played with the value of the sleep and The script is working, then the alarm/timeout is working.

I have then writen the script for the DHCP :

Code:
use Time::HiRes;
use Object::Destroyer;
use Net::DHCP::Watch;
 
 
  # server name
  my $Server = '192.168.1.1';
  # this machine ip and ethernet address
  my $IP     = '192.168.1.2';
  my $Ether  = '00:23:CD:CE:43:DC';
  my $timeout = 5;
 
  my $stat ='';
 
 # Net::DHCP::Watch object
  my $dhcpw = new Net::DHCP::Watch({
                server => $Server,
                client => $IP,
                ether  => $Ether,
		timeout => $timeout
        });
 
  # Open network
  $dhcpw->watch();
 
  my $start = [ Time::HiRes::gettimeofday( ) ];
 
eval {
 
    local $SIG{ALRM} = sub { die("alarm\n") };
 
        alarm(6);
	my $sentry = Object::Destroyer->new( sub {alarm(0)} );
 
	#Get Status
    $stat = $dhcpw ->status;
 
};
 if ($@) {
 
		print "Error";
}
else {
 
  $elapsed = Time::HiRes::tv_interval( $start );
  $elapsed = $elapsed * 1000;
 
  # print results
 
  if ( $stat->{Ok}  ) 
  {  
    print "Remote DHCP $Server online.\n";
	print "Dhcptime;$elapsed;ms";
 }
 else 
 { print "Error";
 }
}
When i put a correct and working DHCP server, the script is working fast and rapidly.

However, when i put a bad DHCP server, the script is blocking at the command "$stat = $dhcpw ->status;". The script won't stop itself even with my timeout.

I don't understand why it's not working, i suppose it's something related to the DHCP module. But i thought that my alarm will take the lead and stop the script after X seconds.

Thank you for your help or for your idea.