Re: problems parsing a DHCP.leases file.



Hans Meier wrote:

Here is a way to process one lease { }
after another, with the possibility to extract every field you want.


A similar approach using Text::Balanced:

C:\tmp>cat tmp.pl
#!/usr/bin/perl

use strict;
use warnings;

use Text::Balanced qw{ extract_bracketed };
use Regexp::Common qw{ net };

$/ = '';

my @leases;
while ( my $lease = <DATA> ) {
my ($extracted,$remainder,$skipped)
= extract_bracketed($lease,'{}','lease [.\d]+ ');

my ($ip) = $skipped =~ m{ lease \s+ ($RE{net}{IPv4}) }x;
my ($hostname) = $extracted =~ m{ client-hostname \s+ "([^"]+)" }x;

push @leases,{ ip => $ip, hostname => $hostname };
}

use Data::Dumper;
print Dumper(\@leases);

__DATA__
lease 10.10.97.207 {
starts 2 2005/12/20 16:10:51;
ends 2 2005/12/20 20:10:51;
tstp 2 2005/12/20 20:10:51;
binding state free;
hardware ethernet 00:0b:97:2b:ea:fe;
uid "\001\000\013\227+\352\376";
client-hostname "HOST1";
}

lease 10.10.97.208 {
starts 2 2005/12/20 16:10:51;
ends 2 2005/12/20 20:10:51;
tstp 2 2005/12/20 20:10:51;
binding state free;
hardware ethernet 00:0b:97:2b:ea:fe;
uid "\001\000\013\227+\352\376";
client-hostname "HOST2";
}

C:\tmp>tmp.pl
$VAR1 = [
{
'ip' => '10.10.97.207',
'hostname' => 'HOST1'
},
{
'ip' => '10.10.97.208',
'hostname' => 'HOST2'
}
];

-jp

.



Relevant Pages

  • How to Set FQDN in DHCPD for DDNS
    ... but only for this print server. ... DHCPOFFER on 192.168.0.216 to 00:c0:02:38:52:31 via eth0 ... lease 192.168.0.216 { ... next binding state free; ...
    (comp.os.linux.networking)
  • How to Set FQDN in DHCPD for DDNS
    ... but only for this print server. ... DHCPOFFER on 192.168.0.216 to 00:c0:02:38:52:31 via eth0 ... lease 192.168.0.216 { ... next binding state free; ...
    (comp.os.linux.misc)
  • Re: I am confused about DHCP
    ... Note that the lease issued on 2008/06/11 doesn't expire until ... Most DHCP servers only ... I see the uid only for Windows computers (don't be fooled by ethernet ...
    (Fedora)
  • Re: Regular Expression Trouble
    ... the sed construct that should do ... It'd be better if you post a few relevant lines of the log file. ... lease 192.168.10.216 { ...
    (freebsd-questions)
  • RE: problems parsing a DHCP.leases file.
    ... lease entry with a closing curly brace and a new line. ... I am able to get the IP address but the $hostname ... use warnings; ...
    (perl.beginners)