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:
HCP:
acket;
use Net:
HCP::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:
HCP:
acket->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:
HCP:
acket->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);
# Client Program
use IO::Socket::INET;
use Net:
use Net:
# 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:
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:
open (MACS, ">> data.txt"); #Response File
print MACS $packet->toString(); # Print to Response File
print $packet->toString(); # Print to screen
sleep (1);
}
close (MYFILE);