O
O
Oleg Voitenko2016-02-03 21:09:24
Perl
Oleg Voitenko, 2016-02-03 21:09:24

How to write a regular expression in perl?

Hi everybody!
I recently started learning perl, so don't butt too hard, as it's still a complete noob.
And so, the crux of the matter. There is a certain apache config file httpd.conf.
The design is, as it were, standard under Centos:

<VirtualHost 222.222.222.222:81 >
        ServerName somedomain.com
        CustomLog /var/www/httpd-logs/somedomain.com.access.log combined
        DocumentRoot /var/www/someuser/data/www/somedomain.com
        ErrorLog /var/www/httpd-logs/somedomain.com.error.log
        ServerAlias www.somedomain.com
        SuexecUserGroup someuser somegroup
        ServerAdmin [email protected]
        ScriptAlias /cgi-bin/ /var/www/someuser/data/www/somedomain.com/cgi-bin/
</VirtualHost>

There are many such virtual hosts.
Here is the task, to find exactly the virtualhost whose ServerName value matches the value of the $domain variable. and for example, remove its section (everything from to ) without hooking the others.
How can I write a regular expression?
The second situation is when I need to find a virtual host by ServerName and replace values ​​such as ServerAlias ​​and ServerAdmin only in this section.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
targumon, 2016-02-03
@targumon

You can use a similar option.
The config is like this:
<VirtualHost 222.222.222.222:81 >
ServerName somedomain.com
CustomLog /var/www/httpd-logs/somedomain.com.access.log combined
DocumentRoot /var/www/someuser/data/www/somedomain.com
ErrorLog /var/www /httpd-logs/somedomain.com.error.log
ServerAlias ​​www.somedomain.com SuexecUserGroup
someuser somegroup ServerAdmin
[email protected]
ScriptAlias ​​/cgi-bin/ /var/www/someuser/data/www/somedomain.com/cgi- bin/
</VirtualHost>
<VirtualHost 222.222.222.222:81 >
ServerName somedomain1.com
CustomLog /var/www/httpd-logs/somedomain1.com.access.log combined
DocumentRoot /var/www/someuser/data/www/somedomain1.com
ErrorLog /var/www/httpd-logs/somedomain1.com.error.log
ServerAlias ​​www.somedomain1.com
SuexecUserGroup someuser somegroup ServerAdmin
[email protected]
ScriptAlias ​​/cgi -bin/ /var/www/someuser/data/www/somedomain1.com/cgi-bin/
</VirtualHost>
<VirtualHost 222.222.222.222:81>
ServerName somedomain2.com
CustomLog /var/www/httpd-logs/somedomain2. com.access.log combined
DocumentRoot /var/www/someuser/data/www/somedomain2.com
ErrorLog /var/www/httpd-logs/somedomain2.com.error.log
ServerAlias ​​www.somedomain2.com
SuexecUserGroup someuser somegroup ServerAdmin
[email protected]
ScriptAlias ​​/cgi-bin/ /var/www/someuser/data/www/somedomain2.com/cgi-bin/
</VirtualHost>

sub removeIfSomedomain($$) {
    my $text = shift;
    my $domain = shift;
    
    $text = '' if $text =~ m{^\s*?ServerName\s+?$domain\s*?$}igsm;
    
    return $text;
}

sub changeContent($$$) {
    my $text = shift;
    my $domain = shift;
    my $new_values = shift;
    
    return $text if $text !~ m{^\s*?ServerName\s+?$domain\s*?$}igsm;
    
    foreach my $key ( keys %$new_values ) {
        $text =~ s{^(\s*?$key\s+?).+?$}{$1$new_values->{$key}}igsm;
    }
    
    return $text;
}

my $config = ... ; # содержимое конфига

$config =~ s{(<VirtualHost[^>]*?>.*?<\/VirtualHost>)}{ removeIfSomedomain($1,'somedomain1.com') }igse;

$config =~ s{(<VirtualHost[^>]*?>.*?<\/VirtualHost>)}{ 
                changeContent($1,'somedomain2.com', { SuexecUserGroup => 'justuser justgroup', 
                                                      ServerAlias => 'www.sd2.com', 
                                                      ServerAdmin => '[email protected]' } ) 
                                                      }igse;

Exhaust:
<VirtualHost 222.222.222.222:81 >
ServerName somedomain.com
CustomLog /var/www/httpd-logs/somedomain.com.access.log combined
DocumentRoot /var/www/someuser/data/www/somedomain.com
ErrorLog /var/www /httpd-logs/somedomain.com.error.log
ServerAlias ​​www.somedomain.com SuexecUserGroup
someuser somegroup ServerAdmin
[email protected]
ScriptAlias ​​/cgi-bin/ /var/www/someuser/data/www/somedomain.com/cgi- bin/
</VirtualHost>
<VirtualHost 222.222.222.222:81 >
ServerName somedomain2.com
CustomLog /var/www/httpd-logs/somedomain2.com.access.log combined
DocumentRoot /var/www/someuser/data/www/somedomain2.com
ErrorLog /var/www/httpd-logs/somedomain2.com.error.log
ServerAlias ​​www.sd2.com
SuexecUserGroup justuser justgroup ServerAdmin
[email protected]
ScriptAlias ​​/cgi -bin/ /var/www/someuser/data/www/somedomain2.com/cgi-bin/
</VirtualHost>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question