简体   繁体   中英

Search string, print line after string is found with further formatting perl

I am currently trying to write a script that determines what is wrong with a website on the server. I am stuck on a variable I am trying to create

my documentroot = 

The document root appears in the apache configuration file /etc/httpd/conf/httpd.conf and looks like this:

<VirtualHost 184.171.253.172:80>
    ServerName lernperl.com
    ServerAlias www.lernperl.com
    DocumentRoot /home/lernperl/public_html

So, basically I decided to use my domain variable $host which will be the domain and search for www.$domain.com , however it is the next line that is important to me. Ideally, I want /home/lernperl/public_html put into a scalar. It seems easy but I have been looking so long for an answer. I appreciate any help! I didn't get very far.

open (configuration_file, "/etc/httpd/conf/httpd.conf");
my @list1 = <configuration_file>;close configuration_file;

The best way to achieve what you are trying to do is to use a regular expression to capture the text that follows "DocumentRoot". To set the contents of a variable using the results of a regular expression, you can do the following:

if ($line =~ /DocumentRoot (.*?)$/) {
    my $variable = $1;
}

More information on perl regular expressions in perl regular expressions tutorial .

Apache config files may contain more than one DocumentRoot and ServerName so your script will need to handle this situation.

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM