Net-DHCPClient cant get timeout
Net-DHCPClient cant get timeout
am 12.09.2006 19:37:06 von Raphael Koechlin
hi there!
i need to know wheter or not a dhcp server is in the net of a certain
interface.
the following script works if there IS a dhcp but if there is NO dhcp
the script hangs ( as i found out in the [ loop $pcap, 1, sub { ]
function.
any ideas how i could get a timeout?
thank you!
raphael
heres my test script:
#!/usr/bin/perl
use Net::DHCPClient;
$macad = '00:0D:B9:03:9F:B9';
my $dhcp = new Net::DHCPClient( macaddr => $macad,
interface => 'eth1',
timeout => 3,
debug => 0,
);
$dhcp->discover( 61 => $macad );
print $dhcp->domain_name();
Re: Net-DHCPClient cant get timeout
am 13.09.2006 12:07:12 von Sisyphus
"Raphael Koechlin" wrote in message
news:1158082626.002932.193490@i3g2000cwc.googlegroups.com...
> hi there!
>
> i need to know wheter or not a dhcp server is in the net of a certain
> interface.
>
> the following script works if there IS a dhcp but if there is NO dhcp
> the script hangs ( as i found out in the [ loop $pcap, 1, sub { ]
> function.
>
..
..
>
> #!/usr/bin/perl
> use Net::DHCPClient;
> $macad = '00:0D:B9:03:9F:B9';
> my $dhcp = new Net::DHCPClient( macaddr => $macad,
> interface => 'eth1',
> timeout => 3,
> debug => 0,
> );
> $dhcp->discover( 61 => $macad );
> print $dhcp->domain_name();
>
It's a good idea to enable warnings - especially if a script is giving
problems. (I don't know whether that will help in this case.) Also possibly
worth establishing at just which point the script hangs.
Does it hang at the new() call, the discover() call or the domain_name()
call ?
Additionally, see perldoc -f alarm.
Assuming that the script hangs on the new call (untested):
-----------
#!/usr/bin/perl
use warnings;
use Net::DHCPClient;
my $macad = '00:0D:B9:03:9F:B9';
alarm(3);
my $dhcp = new Net::DHCPClient( macaddr => $macad,
interface => 'eth1',
timeout => 3,
debug => 0,
);
alarm(0);
$dhcp->discover( 61 => $macad );
print $dhcp->domain_name();
----------
Cheers,
Rob
Re: Net-DHCPClient cant get timeout
am 13.09.2006 14:08:08 von Raphael Koechlin
thanks!
obviously the alarm function is not working correctly on my system. its
an embedded device (wrap).
i also tried it with eval as suggested in
http://perldoc.perl.org/functions/alarm.html
the script hangs at the discover function where net::dhclient scans
incoming packets.
a simple
alarm 2;sleep 10; gives an alarm though.
strange...
is there any other possibility to generate a timeout without the alarm
function?
thanks
> It's a good idea to enable warnings - especially if a script is giving
> problems. (I don't know whether that will help in this case.) Also possibly
> worth establishing at just which point the script hangs.
> Does it hang at the new() call, the discover() call or the domain_name()
> call ?
>
> Additionally, see perldoc -f alarm.
>
> Assuming that the script hangs on the new call (untested):
>
> -----------
> #!/usr/bin/perl
> use warnings;
> use Net::DHCPClient;
> my $macad = '00:0D:B9:03:9F:B9';
>
> alarm(3);
> my $dhcp = new Net::DHCPClient( macaddr => $macad,
> interface => 'eth1',
> timeout => 3,
> debug => 0,
> );
> alarm(0);
> $dhcp->discover( 61 => $macad );
> print $dhcp->domain_name();
> ----------
>
> Cheers,
> Rob
Re: Net-DHCPClient cant get timeout
am 13.09.2006 15:34:30 von Sisyphus
"Raphael Koechlin" wrote in message
..
..
> obviously the alarm function is not working correctly on my system.
Sheesh ... it even works on Win32 (to an extent :-)
..
..
>
> the script hangs at the discover function where net::dhclient scans
> incoming packets.
So ... does "if($dhcp)" return true ? ... even if there's no dhcp ?
I'm thinking it does - otherwise $dhcp->discover() would throw an error
about not being able to call method discover. But I'm not entirely sure
about that, and if it turns out that $dhcp is a false value, then maybe
there's the key to determining that there's no dhcp server available.
ie you could just write:
my $dhcp = new Net::DHCPClient( macaddr => $macad,
interface => 'eth1',
timeout => 3,
debug => 0,
) or die "No dhcp server available";
Next bright idea:
We find that sub discover() looks like this:
-----------------------
sub discover {
my $self = shift;
my %options = @_;
$self->ciaddr( 0 );
$self->yiaddr( 0 );
$self->siaddr( 0 );
$self->giaddr( 0 );
$options{53} = '1';
$self->doit( \%options );
}
-----------------------
You could insert debug print statements within it to determine just where
the hang occurs. (I don't know if that would achieve anything.)
Finally, you could peruse
http://search.cpan.org/search?query=dhcp&mode=dist to see if there's a
better alternative to Net::DHCPClient. All of the other modules listed there
are more recent than Net::DHCPClient.
I think that's about it, for my bright ideas :-)
> a simple
> alarm 2;sleep 10; gives an alarm though.
'perldoc -f alarm' warns about mixing 'alarm' and 'sleep' calls. (But, yes
.... that's about the extent to which it works on Win32 :-)
Cheers,
Rob