Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Parsing external config file

by ybiC (Prior)
on Jan 21, 2001 at 14:28 UTC ( [id://53317]=perlquestion: print w/replies, xml ) Need Help??

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

I'm cleaning up an old script with newfound knowledge.   Part of this cleanup is to move user-modified parameters to an external file.

&CONFEXT is supposed to populate $timeout, @targets, and @command from the file ciscoconf.conf.   As the Dumps show, CONFINT isn't fetching all the info into the appropriate arrays.   I think my "push" syntax is good, and suspect my control structure is *not*.   Would any wise monk(s) care to show me the error of my ways?

  • ciscoconf.pl - the script itself
    • CONFINT - *working* function that reads config parameters from within script itself
    • CONFEXT - *broken* function that tries to read same config parameters from external config file
  • ciscoconf.conf - external config file
  • ciscoconfint.dump - Data::Dumper output from CONFINT
  • ciscoconfext.dump - Data::Dumper output from CONFEXT

Hopefully relevant sections of code follow:
    tired groggy and quite loopy,
    Don
    striving for Perl Adept
    (it's pronounced "why-bick")

###################################################################### +### # Begin small slice of ciscoconf.pl use Data::Dumper; #only for testing use vars qw( $section $timeout @timeout $target @targets $command @commands ); my $conf = 'ciscoconf.conf'; # list of target devices and +commands my $tmp = 'ciscoconf.tmp'; my $log = 'ciscoconf.log'; #&CONFINT(); &CONFEXT(); ---------------------------------------------------------------------- +--- sub CONFINT { $timeout = '15'; @targets = ('192.168.1.27','172.16.0.138' ); @commands = ( 'sho clo', 'set leng 0', 'term leng 0', 'sho clo', 'disa', ); open (DUMP, "> ciscoconfint.dump") or die "Error opening ciscocon +fint.dump for write: $!"; print DUMP Dumper(@timeout), "\n"; print DUMP Dumper(@targets), "\n"; print DUMP Dumper(@commands), "\n"; close (DUMP) or die "Error closing ciscoconfint.dump after write: + $!"; } ---------------------------------------------------------------------- +--- sub CONFEXT { open (CONF, "<$conf") or die "Error opening $conf for read: $!"; while (<CONF>) { next if /^#/ or /^s.*$/ or /^!/; # ignore comment lines chomp; if (/^\[([^\]]+)\]/) { $section = $1; next; } else { if ($section =~ /timeout/i) { push @timeout,$_; } if ($section =~ /targets/i) { push @targets,$_; } if ($section =~ /commands/i) { push @commands,$_; } } } close (CONF) or die " ciscoconf.pl: Error closing $conf after re +ad: $!"; open (DUMP, "> ciscoconfext.dump") or die "Error opening ciscocon +fext.dump for write: $!"; print DUMP Dumper(@timeout), "\n"; print DUMP Dumper(@targets), "\n"; print DUMP Dumper(@commands), "\n"; close (DUMP) or die "Error closing ciscoconfext.dump after write: + $!"; } # End small slice of ciscoconf.pl ###################################################################### +### # begin ciscoconf.conf [Timeout] 15 [Targets] 192.168.1.27 172.16.0.138 [Commands] sho clo set leng 0 term leng 0 disa sho clo # end ciscoconf.conf ###################################################################### +### # begin ciscoconfext.dump $VAR1 = 15; $VAR2 = ''; $VAR1 = '192.168.1.27'; $VAR2 = '172.16.0.138'; $VAR3 = ''; $VAR1 = 'term leng 0'; $VAR2 = 'disa'; # end ciscoconfext.dump ###################################################################### +### # begin ciscoconfint.dump $VAR1 = 15; $VAR1 = '192.168.1.27'; $VAR2 = '172.16.0.138'; $VAR1 = 'sho clo'; $VAR2 = 'set leng 0'; $VAR3 = 'term leng 0'; $VAR4 = 'sho clo'; $VAR5 = 'disa'; # end ciscoconfint.dump ###################################################################### +###

Replies are listed 'Best First'.
Re: Parsing external config file
by mirod (Canon) on Jan 21, 2001 at 14:57 UTC

    Your problems come from this line:

    next if /^#/ or /^s.*$/ or /^!/; # ignore comment lines

    and more precisely from the /^s.*$/ part. There you ignore all lines starting with 's' as comments, hence the missing commands (sho clo, set leng 0' and sho clo again. On the other hand empty (all space) lines are happily processed, hence the extra empty items in your lists

    I think you meant /^\s*$/, at least it works with it!

      Bullseye, mirod - that was it exactly.   Thanks a bunch!
          cheers,
          Don
          striving for Perl Adept
          (it's pronounced "why-bick")
Re: Parsing external config file
by BoredByPolitics (Scribe) on Jan 21, 2001 at 18:17 UTC
    Not an answer to your question, but I would really recommend you investigate the AppConfig module. Makes changing/adding parameters at a later date extremely easy. It also handles both config files and commandline options.

    Pete

      Thanks for the suggestion, BBP.   I was going to do that very thing a while back, and researched as many config file parsers as I could find.  

      A new twist has cropped up though.   It would simplify things a lot if I could include variable names for new passwords and community strings in the external config file.   Not the passwords themselves, mind you (the script prompts for and retains them in RAM).   I believe the term is "variable interpolation".

      Do you know if AppConfig would allow that?   Or should I look at something like doing Perl code as a config file?   Ideally, it would go something like this, like templating I suppose:

      [SetPass] conf t enable pass $newen line con 0 pass $newpass login line aux0 pass $newpass login line vty 0 4 pass $newpass login
          cheers,
          Don
          striving for Perl Adept
          (it's pronounced "why-bick")

      Update: Hmmm... that sounds *very* interesting BoredByPolitics.   /me wanders off to take a look...

        Yes!

        You can define 4 types of config variable -

        • Boolean ("varname!")
        • Scalar ("varname=s")
        • Array ("varname=@")
        • Hash ("varname=%")
        So your line enable pass $newen would be best handled as a hash, which would be called enable.

        [section] has the effect of prefixing the varname with the section name, so login would become SetPass_login.

        Multiple assignments to the same array or hash add further elements.

        Both environment varibles, and those previously defined in the config file, can be expanded as values.

        Take a look at the pods for AppConfig::State and AppConfig::File for more details.

        Overall I'm very impressed with this module - it has definately saved me alot of time with the program I'm currently writing.

        Pete

Log In?
Username:
Password:

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

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

    No recent polls found