reading a httpd.conf file

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. #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...
    2. #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: ...
    3. 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...
    4. 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,...
    5. #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...
  3. #2

    Default Re: 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;
    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:

    [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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139