Ask a Question related to PERL Beginners, Design and Development.
-
Chad Kellerman #1
reading a httpd.conf file
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]webmaster@domain1.com[/email]
ServerName domain1.com
User lee
</VirtualHost>
<VirtualHost domain4.com>
User bob
DocumentRoot /usr/www/htdocs/bob/domain1-www
ServerAdmin [email]bob@domain4.com[/email]
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;
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>------------------------------------------
But this just doesn't work. I get the last VirtualHost sections
servername (minus the .com) and the Docroot. But the username isn't
there..
Can anyone steer me in the right direction?
THanks,
Chad
Chad Kellerman Guest
-
#39800 [NEW]: Windows installer does not remove PHP hooks in httpd.conf
From: php at ahlenstorf dot ch Operating system: Windows XP SP2 PHP version: 5.2.0 PHP Bug Type: *Configuration Issues Bug... -
#15872 [Opn->Bgs]: php_admin_value disable_functions does not work when set in httpd.conf
ID: 15872 Updated by: iliaa@php.net Reported By: jr-phpbugs at 3Rcom dot cz -Status: Open +Status: ... -
Apache httpd.conf for distributed mode
Hi, I plan to have CFMX application server under Win2003 and Apache 1.3 web server under Linux. (we have today the same with CF5 and it works... -
configuring httpd.conf on linux to use port 80
Hi ti all, i have a linux fedora box with apache running on, and I want to make tunneling possible with FCS i have of course two IPs avaiable,... -
#24702 [NEW]: disable_functions + httpd.conf should not set value
From: philip at cornado dot com Operating system: linux PHP version: 4.3.3RC1 PHP Bug Type: PHP options/info functions Bug... -
Wiggins D Anconia #2
Re: reading a httpd.conf file
In the above regex you are looking for \d+ as the username, but your> 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]webmaster@domain1.com[/email]
> ServerName domain1.com
> User lee
> </VirtualHost>
>
> <VirtualHost domain4.com>
> User bob
> DocumentRoot /usr/www/htdocs/bob/domain1-www
> ServerAdmin [email]bob@domain4.com[/email]
> 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;
usernames consist of letters?? [A-Za-z]+ maybe more appropriate, or
maybe even \w+?
Have you considered CPAN, Apache::Admin::Config looks promising:> 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>------------------------------------------
>
[url]http://search.cpan.org/~rsoliv/Apache-Admin-Config-0.91/lib/Apache/Admin/Config.pm[/url]
[url]http://danconia.org[/url]
Wiggins D Anconia Guest



Reply With Quote

