Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

Perl Script to edit httpd.conf and changes few options

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
xBellox
Asked:
xBellox
  • 2
  • 2
  • 2
  • +1
4 Solutions
 
mrjoltcolaCommented:
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
 
Adam314Commented:
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
 
ghostdog74Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
xBelloxAuthor Commented:
@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
 
Adam314Commented:
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
 
mrjoltcolaCommented:
>>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
 
xBelloxAuthor Commented:
It gave me a clue...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now