Need help with LEASEQUERY..

roger murray romu42 at gmail.com
Fri May 21 18:21:23 UTC 2010


Hey,

This code works for me to query a CNR DHCP server (Cisco's DHCP), I
haven't tested it with ISC, but might have the opportunity next week.
I use it too loop through 3 servers to find out which server a client
is on. 99% of the code is based on:
http://search.cpan.org/~shadinger/Net-DHCP-0.66/lib/Net/DHCP/Packet.pm
and the example Sending a LEASEQUERY (provided by John A. Murphy).
Hopefully it will be a little help.

Best regards,

Roger Murray

  #!/usr/bin/perl
  # Simple DHCP client - send a LeaseQuery (by mac and receive the IP)
and receive the response
  use strict;
  use warnings;
  use IO::Socket::INET;
  use Net::DHCP::Packet;
  use Net::DHCP::Constants;

  my $usage = "usage: $0 DHCP_CLIENT_MAC\n"; $ARGV[0] || die $usage;

 # loop through cnr-A-F looking for the client.
my $cnr;
#foreach $cnr (qw/ 1 2 3 4 5 6 /) {
foreach $cnr (qw/ 1 3 5 /) {
        my $cnrip = "10.125.$cnr.11";
  # create a socket
        my $handle = IO::Socket::INET->new(Proto => 'udp',
                                  Broadcast => 1,
                                  PeerPort => '67',
                                  LocalPort => '67',
                                  Timeout => '1',
                                  PeerAddr => $cnrip)
                or die "socket: $@";     # yes, it uses $@ here

  # create DHCP Packet
        my $inform = Net::DHCP::Packet->new(
                      op => BOOTREQUEST(),
                      Htype  => '0',
                      Hlen   => '6',
                      Ciaddr => '0',
                      Chaddr => $ARGV[0],
                      Giaddr => $handle->sockhost(),
                      Xid => int(rand(0xFFFFFFFF)),     # random xid
                      DHO_DHCP_MESSAGE_TYPE() => DHCPLEASEQUERY
                      );

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

  #receive response
        $handle->recv(my $newmsg, 1024) or die;
        my $packet = Net::DHCP::Packet->new($newmsg);
  # this will print out the entire packet
        # print $packet->toString();
# this will take the return packet and tell you which cnr it is on.
        my $package = $packet->toString();
                if ( $package =~ /ciaddr = 0.0.0.0/ ){
                        #print "not on $cnrip \n";
                }elsif ( $package =~ /ciaddr =
([\d]+)\.([\d]+)\.([\d]+)\.([\d]+)/) {
                        print "$ARGV[0] has ip $1\.$2\.$3\.$4 and is
on $cnrip \n"
                }
}



On Fri, May 21, 2010 at 3:37 PM, Jiri Popelka <jpopelka at redhat.com> wrote:
> On 05/20/2010 06:04 PM, Pat Winn wrote:
>>
>> Matt,
>> That codebase is (rather ironically) what I stumbled across
>> and was using for a baseline when I started hacking away at this.
>>
>> I'm in the same boat as you...wanting to pull lease info from the
>> running daemon rather than looking at what is in the leases file.
>>
>
> Same here.
> If you find (or succeed in writing) some working
> LEASEQUERY sending piece of software (in whatever language)
> I would very much appreciate to see it, because I'm also in need of it.
> Thanks
>>
>> We are an isp and are in the process of switching from using radius
>> with user authentication for our dialup and dsl customers to DHCP.
>> As such, I need all the info I can glean for each session/lease for
>> logging purposes and some of the info we need isn't in the leases
>> file..so it's down to pulling from the running daemon.
>>
>> I've tried forking an omshell process to talk to the daemon and
>> pull the lease info back into PHP. It worked fine except that every
>> so often the omshell process hangs while connected to the daemon
>> and won't disconnect. When that happens, the daemon stops responding
>> to any new requests. Guessing it puts a lock on it's internal lease
>> objects while omshell is connected since it can alter things and
>> simply won't work until it disconnects...so that becomes an unusable
>> option (short of patching ISC's code which I'd rather not tamper with).
>>
>> I've considered writing my own version of omshell in php but then
>> figured out I could send a leasequery packet to the running daemon
>> and get the same info back...without being connected to it like
>> omshell. So now it's down to...create a PHP class which can send and
>> receive a packet to get the info back that way and be able to re-use
>> said class in whatever php code I want to use it in.
>>
>> That is...IF..I can ever get dhcpd to acknowledge that it received
>> a lease query packet and actually respond to it.  :-(
>>
>> I'm totally open for suggestions at this point, and open to working
>> in a collaborative fashion as well. Perhaps we could both benefit
>> from it.  ;-)
>>
>> Thanks for your time and response!
>>
>>
>
> _______________________________________________
> dhcp-hackers mailing list
> dhcp-hackers at lists.isc.org
> https://lists.isc.org/mailman/listinfo/dhcp-hackers
>



More information about the dhcp-hackers mailing list