Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DHCP script will not terminate upon "no response"

Status
Not open for further replies.

msobie64

ISP
Apr 21, 2008
1
US
Hey guys, I have written a script imports a list of mac addresses and sends a dhcp lease query for every mac address in the file and writes the response to a seperate file. However, if I do not get a response from the DHCP server my script hangs and does not continue through the list. I can not seem to get around this any help would be appreciated. My script is below:

# Client Program
use IO::Socket::INET;
use Net::DHCP::packet;
use Net::DHCP::Constants;
# Create a new socket
$handle=IO::Socket::INET->new(Proto => 'udp',
Broadcast => 1,
PeerPort => '67',
LocalPort => '67',
PeerAddr => '10.24.193.39')
or die "socket: $@"; # yes, it uses $@ here

open (MYFILE, 'macs.txt'); # Mac address list
while (<MYFILE>) {
chomp;
# create DHCP Packet
$inform = Net::DHCP::packet->new(
op => BOOTREQUEST(),
Htype => '0',
Hlen => '6',
Ciaddr => '0',
Chaddr => $_,
Giaddr => $handle->sockhost(),
Xid => hex(FFFFFFFF), # random xid
DHO_DHCP_MESSAGE_TYPE() => DHCPLEASEQUERY


);
# send request
$handle->send($inform->serialize()) or die "Error sending LeaseQuery: $!\n";

#receive response
$handle->recv($newmsg, 1024) or print "$handle not found";
$packet = Net::DHCP::packet->new($newmsg);
open (MACS, ">> data.txt"); #Response File
print MACS $packet->toString(); # Print to Response File
print $packet->toString(); # Print to screen

sleep (1);
}

close (MYFILE);
 
According to the doc on CPAN, you can pass in Timeout => nn on the constructor for the socket. It doesn't say if it's in seconds, milliseconds, or what. You will just have to experiment...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top