Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Config file parsing tutorial?

by tombmbdil (Beadle)
on Feb 17, 2003 at 21:26 UTC ( [id://236122]=perlquestion: print w/replies, xml ) Need Help??

tombmbdil has asked for the wisdom of the Perl Monks concerning the following question:

Wise Monks,

I spend much of my time mucking about with dhcp data. I'm also in the process of trying to learn OO perl. So, I figured I would practice my class-building skills by creating a DHCP module for myself which will gather various lease/network information.

I find parsing the dhcpd.leases file to be pretty easy because the data is nice and predictable. The human-generated dhcpd.conf file, on the other hand, is a nightmare. I have figured out a way to gather some of the information I need, but I know there must be some established algorithm that is much more elegant. I've browsed a few of the configuration file parsing modules on cpan to search for ideas, but either they aren't sufficiently commented, or they deal with simpler configuration schemes (as far as I can tell).

Does anyone know of an online tutorial somewhere that covers configuration file parsing algorithms?

The current bit of code I have works for what I need right now, but I know it's extremely oversimplified and prone to break under various conditions.

Here's a simplified snippet of what I have:

sub new { my $invocant = shift; my $class = ref($invocant) || $invocant; my $self = { # Paths for the leases and conf files lease_file => '/var/state/dhcp/dhcpd.leases', conf_file => '/etc/dhcpd.conf', # Hash keyed by dhcpd.conf configuration option # Values contain references to subroutines for handling # each option conf_fields => { host => \&_host, # Static hosts }, # Hash keyed by MAC address # Values contain array of lease arrays leases => { }, @_, }; # Load static hosts &_init_conf($self); bless ($self, $class); return $self; } # Initialize static hosts sub _init_conf { my $self = shift; open CONF, $self->{'conf_file'} or die "Couldn't open ", $self->{'conf_file'}, " $!"; + my $option; while (<CONF>) { # If the current line contains an open bracket grab the first +word # as the option key $option = $1 if $_ !~ /^\s*#/ && /^\s*(\w+).*{/; # Call the subroutine associated with the current option # Must eval this call in case no such subroutine exists. eval { $self->{'conf_fields'}{$option}->($self, $_) } if $opti +on; # Clear option key if closing bracket $option = undef and eval { $self->{'conf_fields'}{$option}->($ +self, $_) } if $_ !~ /^\s*#/ && /}/; } close CONF; } # Method to handle static host declarations { my $mac, $ip; sub _host { my $self = shift; # Current line of dhcpd.conf $_ = shift; # Grab MAC address $mac = mac_normal($1) if /\s*hardware ethernet (\w+:\w+:\w+:\w ++:\w+:\w+)/; # Grab IP $ip = $1 if /\s*fixed-address (\d+.\d+.\d+.\d+)/; # Push entry onto array of leases for host's MAC # and clear $mac, $ip variables push @{$self->{'leases'}{$mac}}, [ $ip, 'static'] and ($ip, $m +ac) = ('', '') if /}/ and $ip && $mac; } }

--------------------------
Sample Static Host:

host camel { hardware ethernet 00:AA:12:BB:A1:34; filename "/default.cfg"; fixed-address 10.1.2.3; }

Replies are listed 'Best First'.
Re: Config file parsing tutorial?
by steves (Curate) on Feb 17, 2003 at 21:54 UTC

    You might be able to get the data you need directly from the DHCP server instead of parsing config. files by using Net::DHCPClient.

Re: Config file parsing tutorial?
by Koschei (Monk) on Feb 18, 2003 at 10:48 UTC

    I was doing some parsing in a similar domain the other year. I was parsing a file of info from which I wanted to create a dhcpd.conf file =)

    I found Parse::RecDescent to be an excellent parser. I thoroughly recommend it. It may not be the swiftest module on the block, but it's certainly one of the most powerful (well, until Damian releases his perl6-style parser). I found the speed acceptable since changing my conf wasn't something I did every month.

    -- Iain, aka Spoon.

Re: Config file parsing tutorial?
by traveler (Parson) on Feb 17, 2003 at 23:43 UTC
    The file dhcpd-lib.pl that comes with Webmin (Webmin is BSD License) is a perl library for parsing the dhcpd.conf file. Also I found this on google. That should get you started.

    --traveler

      Those were good links, thanks.
      The code in the google link isn't really generic enough for what I need, but I've been looking at the Webmin code and hopefully I can pull out some of the concepts if I stare at it long enough.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://236122]
Approved by Coruscate
Front-paged by Coruscate
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-25 20:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found