Solved

Perl Script to edit httpd.conf and changes few options

Posted on 2009-04-03
7
412 Views
Last Modified: 2012-05-06
Hi,

  I have experience with programming (PHP, javascript, Delphi)... But I'm new on Perl Programming. What I need is one script that opens one file modify some block of text and save it.

   More specifically, I need to edit the apache httpd.conf modify 3 or 4 lines and save it. Here is the before and after:


BEFORE:

~~~~ some block of text ~~~~

<VirtualHost 255.255.255.0>
DocumentRoot /home/./username/documentroot
ServerName www.domainname.com.br
RewriteEngine On
<Directory /home/./username/documentroot>
    AddType application/x-httpd-php .php .php3
    php_admin_flag safe_mode On
    php_admin_value include_path ".:/tmp/:/home/./username/documentroot"
    php_admin_value open_basedir ".:/tmp/:/home/./username/documentroot"
    php_admin_value safe_mode_include_dir /home/./username/documentroot
</Directory>
</VirtualHost>

~~~~ the rest of file ~~~~

AFTER:

~~~~ some block of text ~~~~~~~

<VirtualHost 255.255.255.0>
DocumentRoot /home/./username/documentroot
ServerName www.domainname.com.br
RewriteEngine On
CustomLog /home/./username/logs/access_log combined   <--- new line
<Directory /home/./username/documentroot>
    AddType application/x-httpd-php .php .php3
    php_admin_flag safe_mode On
    php_admin_value include_path ".:/tmp/:/home/./username/documentroot"
    php_admin_value open_basedir ".:/tmp/:/home/./username/documentroot
    php_admin_value safe_mode_include_dir /home/./username/documentroot
</Directory>
<Directory /home/./username/documentroot/stats>    <--- new line
    AllowOverride AuthConfig     <--- new line
</Directory>    <--- new line
</VirtualHost>


~~~~ the rest of file ~~~~


But the is more... Before insert the new lines I must be sure that they doesn't exists already.


Please help, thanks :)

0
Comment
Question by:xBellox
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 200 total points
Comment Utility
I could take a stab at a script to do that, but I'm sure someone will be along to post one soon enough. In the meantime, I would at least investigate a Perl module that is already written to manage Apache config. There is almost always something on CPAN. One downside is it requires a compiler to build, but builds fine on Linux / UNIX boxes with GCC installed.

cpan install Apache::Admin::Config

0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 225 total points
Comment Utility
For complex processing, using a module as mrjoltcola suggests would be good.  If you just need to add a few lines, here is a script that'll do it.

I suggest you make a backup before running this.
You will also need write permission to the file.

#!/usr/bin/perl
 

#NOTE: Update file path/name as needed

open(my $fh, "+<", "/etc/apache2/httpd.conf") or die "Could not open file: $!\n";

my @lines=<$fh>;

seek($fh, 0, 0);

foreach (@lines) {

    print $fh $_;

    print "CustomLog /home/./username/logs/access_log combined\n" if /RewriteEngine On/;

    print "<Directory /home/./username/documentroot/stats>\n"

        . "  AllowOverride AuthConfig\n"

        . "</Directory>\n" if m|</Directory>|;

}

close($fh);

Open in new window

0
 
LVL 9

Assisted Solution

by:ghostdog74
ghostdog74 earned 75 total points
Comment Utility
since you have experience in PHP, you can do that in PHP, not necessary Perl. (unless its an absolute must)


$file=fopen("apache.config_path","r");

$newstring = <<<END

<Directory /home/./username/documentroot/stats>  

    AllowOverride AuthConfig 

</Directory>

END;

while ( ($line = fgets($file,4096)) !==FALSE) {

  if ( strpos($line,"RewriteEngine") !== FALSE) {

   print $line."CustomLog /home/./username/logs/access_log combined\n";

   continue;

  }else if (strpos($line,"</Directory>") !==FALSE) {

   print $line.$newstring."\n";

  }else{

   print $line;

  }

}

   fclose($file);

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 9

Author Comment

by:xBellox
Comment Utility
@mrjoltcola:

   Like I said, I'm new on perl scripting, I need more help than that. Thank you.


-------------------------

@Adam314:

   The script I need it's more complex than add lines at the end of the file. I'm googling "mrjoltcola"...

--------------------------

@ghostdog74

 I need to be in perl cause it will run inside another perl script. Thank you anyway.
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 225 total points
Comment Utility
The documentation on the module to help is here:
    http://search.cpan.org/~rsoliv/Apache-Admin-Config-0.94/lib/Apache/Admin/Config.pm

This module is designed to help you read and make changes to apache config files.  If the changes you need to make are complex, using this module will help.  The changes you've requested so far are not complex, and can be made with a script like what I posted.

If you tell us what changes are needed, we can help direct you to the best solution.  So far, I'd say there is no need for the module - you can just add the lines to the file.
0
 
LVL 40

Expert Comment

by:mrjoltcola
Comment Utility
>>I'm googling "mrjoltcola"

Don't do that, you don't want any of my modules for this! :)

If you aren't comfortable installing the module I mentioned, I would stick with Adam's solution, he will get you what you need.
0
 
LVL 9

Author Closing Comment

by:xBellox
Comment Utility
It gave me a clue...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now