I need to parse dhcpd.leases to store data in mysql

markd mark at immermail.com
Fri Jun 30 01:46:45 UTC 2006


Sébastien CRAMATTE wrote:
>> $/ = "}\n";
>> while (<>) {
>>      ($starts) = /starts \d ([^;]+)/;
>>      ($ends) = /starts \d ([^;]+)/;
>>      ($state) = /binding state ([^;]+)/;
>>      ($mac) = /hardware ethernet ([^;]+)/;
>>
>>      $insert->execute($starts, $ends, $state, $mac);
>> }
>>

> 
> But how do you parse the lease ?
> Could you give me a complete example / script ?

I'm not sure I understand the question. The code above
is parsing the lease.  But "parse lease" is sort of vague.
What data do you want to get out of the lease?  What format
do you want it in?

Here's a complete script that prints IP, MAC and start time
of active leases:

#!/usr/bin/perl

sub parse_lease {
    local $_ = shift;
    my %l;
    ($l{ipaddr}) = /lease (\S+)/;
    ($l{starts}) = /starts \d ([^;]+)/;
    ($l{mac}) = /hardware ethernet ([^;]+)/;
    ($l{state}) = /binding state ([^;]+)/;
    return \%l;
}

open(LEASES, "dhcpd.leases");
$/ = "}\n";

while (<LEASES>) {
    my $lease = parse_lease($_);
    next unless $lease->{state} eq 'active';
    printf "ip=%s mac=%s starts=%s\n",
            $lease->{ipaddr}, $lease->{mac}, $lease->{starts};
}


mark



More information about the dhcp-users mailing list