Hans,

This script works really well but, I am a bit confused on what you are doing
with this: local $/="}\n";  I have not seen local used much as I thought it
was replaced by "my".  It almost looks like you are defining the end of each
lease entry with a closing curly brace and a new line.  Does the dollar sign
indicate that is the end of the input?

As for the regex matches the first two make sense to me but I am a bit
confused on the third one
my ($client_hostname)=$record=~/^\s+client-hostname\s+"([\w.-_]+)"/m

I can see that we are creating a variable called $client_hostname which is
defined by a match to $record which is feed in by the filehandle.  I see
that we are searching for a line starting with one or more spaces followed
by client-hostname then one or more spaces followed by one word character
and anything else but what does the -_ do? And what does the m on the
outside do?

Thanks again for the example it is really interesting and has helped me.

-angus


-----Original Message-----
From: Hans Meier (John Doe) [mailto:[EMAIL PROTECTED] 
Sent: Monday, February 27, 2006 12:53 AM
To: beginners@perl.org
Subject: Re: problems parsing a DHCP.leases file.

Angus am Montag, 27. Februar 2006 08.25:
> Hi all,
>
>
>
> I am having some problems filling a variable based on the contents of a
> dhcpd.leases file.  All I want at this time is the hostname and ip
address.
> My eventual goal is to create hash of hashes with this information but for
> now I just want to read in the file and see that I have defined my
> variables correctly.  I am able to get the IP address but the $hostname
> variable is always undefined.  The syntax for any given host in a leases
> file looks like this:
>
>
>
> 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";
>
> }
>
>
>
> Here is what I have so far.
>
>
>
> #!/usr/bin/perl
>
> #
>
> use strict;
>
> use warnings;
>
>
>
> my $dhcp_data = "dhcpd.leases";
>
>
>
> my %dhcpd;
>
> my $ip;
>
> my $hostname;
>
>
>
> {
>
> open (DHCPD, $dhcp_data) || die "Can't open $dhcp_data $!\n";
>
>
>
> while (my $line = <DHCPD>) {
>
> next if ($line =~ /^\s*$/ or # blank line
>
>              $line =~ /^\s*#/ );
>
>
>
> if ($line =~ /^lease (\d+\.\d+\.\d+\.\d+)/) {
>
>             $ip = $1; }
>
> elsif ($line =~ /^client-hostname/) {
>
>             $hostname = $1; }
>
> else {next;};
>
> print "I found IP:$ip\n";
>
> print "I found Hostname: $hostname\n";
>
>             }
>
> }

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

I think it is easy to read, understand, and alter.

=====
#!/usr/bin/perl
use strict;
use warnings;


local $/="}\n"; # <<<<< look here!
while (my $record=<DATA>) {

  #print "*** $record ***"; # for debugging record extracting

  my ($lease)=$record=~/lease\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/;
  my ($binding_state)=$record=~/^\s+binding\s+state\s+(\w+)/m;
  my ($client_hostname)=$record=~/^\s+client-hostname\s+"([\w.-_]+)"/m;

  print "lease '$lease' (host '$client_hostname') has ".
         "binding state '$binding_state'\n";
}




__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";

}
=====

This prints out:

lease '10.10.97.207' (host 'HOST1') has binding state 'free'
lease '10.10.97.208' (host 'HOST2') has binding state 'free'


hth,
Hans

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




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


Reply via email to