Hello,

Follow the conditions,the resolving way is not so complicated as yours.I would give my sample way,hope it's useful to you.

here is the config file's content:
================
$ cat config.txt
;test file
# comment lines

IP = 1.2.3.4
PORT = 80
PREFORK_CHILDS = 5
MIN_SPARE_CHILDS

here is the sample script:
================
$ cat test.pl
use strict;
use Data::Dumper;
my %hash;

open HD,"config.txt" or die $!;
while(<HD>){
   next if /^\s*#|^\s*;/;
   next if /^\s*$/;

   my ($key,$value) = split/=/;
   $key =~ s/^\s+|\s+$//g;
   $value =~ s/^\s+|\s+$//g;

   $hash{$key} = $value || 'nodefined';
}
close HD;

print Dumper \%hash;

__END__


Run it and get the result output:

$ perl test.pl
$VAR1 = {
         'PREFORK_CHILDS' => '5',
         'IP' => '1.2.3.4',
         'MIN_SPARE_CHILDS' => 'nodefined',
         'PORT' => '80'
       };


From: "Gregory Machin" <[EMAIL PROTECTED]>
To: beginners@perl.org
Subject: regex and parsing config file directives..
Date: Fri, 14 Jul 2006 11:18:40 +0200

Hi.
Sorry to bother but I can't get this script to work..
It is supposed to parse the openvpn config,
1) any line starting with a ";" is to be ignored
2) all directives are written to a hash where the key is the directive and
the value is the  value of the  directive .
3) if the directive is present but no value is set then the key must be set
to the directive and the value must be set to "defined" or the same value as
the key..

this is the script so far


#!/usr/bin/perl
#

use strict;

# vars
my @tmp_load_file;
my @tmp_directive;
my %directive;
#
sub get_config_file {
my $line; #used in while for outputing each line.
print "opening config file for processing\n\n";
open(IN, "< /etc/openvpn/client.conf");

while (<IN>){

  if (( $_ =~ /^#/ ) || ( $_ =~ /^\ /) || ( $_ =~ /^\n/) ){
  } else {
  chomp;         push(@tmp_directive , $_ );
          }
}

close (IN);
}

get_config_file;



sub config_hash {
      ##### regex used in the function
      ## /.+\s(.+) used to find the option set for that directive. "dev
tun" will return "tun"
  my $value ;
foreach (@tmp_directive) {
  #print "$_";       if ( $_ != /^\;/) {
  } else {
      /^([\w|-]+)\s(.+)/ ;
      print " $1 => $2 \n";
      $directive{$1} = $2 ;
  }        if ( $_ != /^\;/) {
  } else {
      /^([\w|-]+)\n/ ;
      #print ">> $1 => $1 \n";
      #$value = "1" unless $2;
      $directive{$1} = $1 ;
  }      }
}


config_hash ;

my $key; my $value;#

print ">>>>>>>>>>>>>>>\n\n";
for $key (keys %directive){
 $value = $directive{$key};
 print "$key => $value\n";
}

this it the output..
[EMAIL PROTECTED] openvpn]$ perl passconfig.pl
opening config file for processing

=>
dev => tun0
proto => tcp
remote => 192.168.1.1 1194
resolv-retry => infinite
=>
=>
=>
ca => ca.crt
cert => greg.crt
key => greg.key
cipher => AES-128-CBC
=>
verb => 3


=>
proto => tcp
cert => greg.crt
dev => tun0
ca => ca.crt
key => greg.key
remote => 192.168.1.1 1194
verb => 3
cipher => AES-128-CBC
resolv-retry => infinite



Many Thanks
Greg



--
Gregory Machin
[EMAIL PROTECTED]
www.linuxpro.co.za



--
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