Re: Record separator and regex switch



Beginner am Montag, 26. Februar 2007 14:50:
Hi,

Hi

I am trying to parse some dhcp-lease files to extract the ip, mac and
hostname.

I am struggling to get either, the regex of the $/, correct. I am not
sure which combination of these I should use.

There is some sample data and my best effort below. Can anyone offer
any pointers?

TIA,
Dp.


======= Sample Data =========
[moved to __DATA__ section below]
============== My effort ===========
#!/usr/bin/perl

use strict;
use warnings;

my $file = '/var/lib/dhcp3/dhcpd.leases';
my ($ip,$mac,$host);

#$/ = "}\n";

used below :-)

$/ = '';

open(FH,$file) or die "Can't open $file: $!\n";

while (<FH>) {
chomp;
($ip,$mac,$host) = ($_ =~
/lease\s+(\d{3}\.\d{3}\.\d{3}\.\d+).*thernet\s+(\d{2}:\d{2}:\d{2}:\d{2
}:\d{2}:\d{2}).*ostname\s+\
"(\w+\.scien.*)"/smg);

print "$ip $mac $host\n";

}

To keep the demonstration script short, I use a short regex that should be
more specific

#!/usr/bin/perl

use strict;
use warnings;

{
local $/="}\n";
for (<DATA>) {
my ($ip,$mac,$host)=
/lease\s+(\S+).*
ethernet\s+(\S+);.*
hostname\s+(\S+);
/sx;
print "IP $ip - MAC $mac - HOST $host\n";
}
}

__DATA__
lease 196.222.237.209 {
starts 5 2007/02/23 17:53:57;
ends 6 2007/02/24 17:53:57;
binding state active;
next binding state free;
hardware ethernet 00:60:04:28:28:01;
client-hostname "lab.mydomain.com";
}
lease 196.222.237.209 {
starts 5 2007/02/23 17:53:57;
ends 6 2007/02/24 17:53:57;
binding state active;
next binding state free;
hardware ethernet 00:60:04:38:38:01;
client-hostname "lab.mydomain.com";
}
lease 196.222.237.195 {
starts 5 2007/02/23 17:54:04;
ends 6 2007/02/24 17:54:04;
binding state active;
next binding state free;
hardware ethernet 00:0c:c1:33:31:0d;
uid "\001\000\014\361\3231\015";
client-hostname "puck";
}
----8<----

IP 196.222.237.209 - MAC 00:60:04:28:28:01 - HOST "lab.mydomain.com"
IP 196.222.237.209 - MAC 00:60:04:38:38:01 - HOST "lab.mydomain.com"
IP 196.222.237.195 - MAC 00:0c:c1:33:31:0d - HOST "puck"

Dani
.



Relevant Pages

  • Re: (Newb) No Subnet declaration for start (0.0.0.0)
    ... # All times in this file are in UTC, not your local timezone. ... next binding state free; ... lease 10.7.5.100 { ...
    (linux.redhat)
  • Record separator and regex switch
    ... I am trying to parse some dhcp-lease files to extract the ip, mac and ... lease 196.222.237.209 { ... next binding state free; ...
    (perl.beginners)
  • need some help to understand dhcpd.leases
    ... binding state active and the same hardware address. ... option domain-name "mydomain"; ... next binding state free; ... lease 172.16.19.46 { ...
    (comp.os.linux.networking)
  • Re: Record separator and regex switch
    ... I am struggling to get either, the regex of the $/, correct. ... use strict; ... use warnings; ... next binding state free; ...
    (perl.beginners)
  • Re: Slightly OT: Macs + DHCPd/bind
    ... Clients get IPs from the dhcp server and all. ... The mystery is that only some of them will trigger an update to bind, ... here's a PeeCee lease data: ... next binding state free; ...
    (Fedora)