> Hello everyone,
>
> I have a little issue that I am sure someone has come across here
> once before and was wondering if I can get some pointers.
>
> I am trying to read and grab values from a "messy" httpd.conf
> file. What I am trying to do is grab the ServerName value and
> DocumentRoot value for a given VirtualHost depending on the username I
> have defined.
>
> The httpd.conf file has many VirtualHost sections (50 +). And each
> section does not have the same order of directives as the next. For
> example:
>
> <VirtualHost domain1.com>
> ScriptAlias /cgi-bin/ /usr/www/htdocs/lee/domain1-www/cgi-bin/
> DocumentRoot /usr/www/htdocs/lee/domain1-www
> ServerAdmin [EMAIL PROTECTED]
> ServerName domain1.com
> User lee
> </VirtualHost>
>
> <VirtualHost domain4.com>
> User bob
> DocumentRoot /usr/www/htdocs/bob/domain1-www
> ServerAdmin [EMAIL PROTECTED]
> ScriptAlias /cgi-bin/ /usr/www/htdocs/bob/domain1-www/cgi-bin/
> ServerName domain4.com
> </VirtualHost>
>
>
> So I wanted to write a script that if I have a username I could use
> that to get the ServerName and DocumentRoot from the correct VirtualHost
> Section. Here is what I have:
>
> ----------------------<code>-----------------------------------------
> #!/usr/local/bin/perl
> use strict;
> use warnings;
> use diagnostics;
> $|++;
>
> use vars qw(
> $httpd_conf_dir $httpd_conf_file $owner
> $servername $docroot $user
> );
>
> $httpd_conf_dir = '/etc/httpd/conf';
> $httpd_conf_file = "$httpd_conf_dir/httpd.conf";
> $user = 'lee';
> $/ = '<V';
>
> open (HTTPD, "$httpd_conf_file")
> or die "cannot open $httpd_conf_file:$!";
> while (<HTTPD>)
> {
> if (/irtualHost/)
> {
> $owner = $1 if /^User\s*(\d+)/m;
In the above regex you are looking for \d+ as the username, but your
usernames consist of letters?? [A-Za-z]+ maybe more appropriate, or
maybe even \w+?
> next if ($owner ne $user);
> $servername = $1 if /^ServerName\s*(\w+)/m;
> $docroot = $1 if /^DocumentRoot\s*(.+)/m;
> }
> }
> close (HTTPD);
>
> print "$owner : $servername : $docroot\n";
>
> ------------------</code>------------------------------------------
>
Have you considered CPAN, Apache::Admin::Config looks promising:
http://search.cpan.org/~rsoliv/Apache-Admin-Config-0.91/lib/Apache/Admin/Config.pm
http://danconia.org
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>