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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to