troubleshooting Question

timeout for perl script

Avatar of rangers80
rangers80Flag for Italy asked on
ProgrammingPerlDHCP
5 Comments1 Solution838 ViewsLast Modified:
I need to set up a timeout about this perl script.
The function recv might sent in hang the script if the are no ldhcp server listening.
use IO::Socket::INET;
use Net::DHCP::Packet;
use Net::DHCP::Constants;
my $result='false';

sub prova() {
my $br_addr = sockaddr_in( '67', inet_aton('255.255.255.255') );
my $xid     = int( rand(0xFFFFFFFF) );
my $chaddr  = '00189BF9C1DG';

my $socket = IO::Socket::INET->new(
    Proto     => 'udp',
    Broadcast => 1,
    LocalPort => '68',
) or die "Can't create socket: $@\n";
print "inizio:\n";
my $discover_packet = Net::DHCP::Packet->new(
    Xid                           => $xid,
    Chaddr                        => $chaddr,
    Flags                         => 0x8000,
    DHO_DHCP_MESSAGE_TYPE()       => DHCPDISCOVER(),
    DHO_HOST_NAME()               => 'Perl Test Client',
    DHO_VENDOR_CLASS_IDENTIFIER() => 'perl',
);

$socket->send( $discover_packet->serialize(), 0, $br_addr)
    or die "Error sending:$!\n";
my $buf = '';

$socket->recv( $buf, 4096) or die "recvfrom() failed:$!";
my $resp = new Net::DHCP::Packet($buf);
print "Details:\n" . $resp->toString();
$ip_offerto=$resp->yiaddr();
print $ip_offerto;
$result = ($ip_offerto =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) ? "true":"false";
print $result;
close($socket);
}


my $nowtime = localtime(time);

print "\n";
print "\n";
print $nowtime;
print "\n";

prova();
if ( $result eq 'true') { 
......
}
else
{
------
}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros