×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Apache vhost - block treatment

Apache vhost - block treatment

Apache vhost - block treatment

(OP)
Hi all,

I'm a beginner's awk.
I have a file apache like this:

CODE -->

<VirtualHost *:443>
        ServerName site1.compagny.org
        DocumentRoot /var/www/html/

        SSLEngine on
        SSLProxyEngine on
        SSLCertificateFile /etc/httpd/ssl/company.crt
        SSLCertificateKeyFile /etc/httpd/ssl/company.key
        SSLCertificateChainFile /etc/httpd/ssl/gsdomainvalsha2g2r1.pem

        ProxyPass /stat https://srv.compagny.org/stat
        ProxyPassReverse /stat https://srv.compagny.org/stat
</VirtualHost>

<VirtualHost *:443>
        ServerName site2.compagny.org
        DocumentRoot /var/www/html/

        SSLEngine on
        SSLProxyEngine on
        SSLCertificateFile /etc/httpd/ssl/company.crt
        SSLCertificateKeyFile /etc/httpd/ssl/company.key
        SSLCertificateChainFile /etc/httpd/ssl/gsdomainvalsha2g2r1.pem

        ProxyPass /office https://srv2.compagny.org/office
        ProxyPassReverse /office https://srv2.compagny.org/office
        ProxyPass /home https://srv2.compagny.org/home
        ProxyPassReverse /home https://srv2.compagny.org/home
</VirtualHost>
[...] 

I want parsing block by block, catch ServerName and ProxyPass and print out like this:
Vhost#site1.compagny.org/stat
Vhost#site2.compagny.org/office
Vhost#site2.compagny.org/home

I go step by step and want print just Servname actually, but I'm so bad, this command print only last Servername.
awk '/<VirtualHost/,/<\/VirtualHost>/ { if ($0 ~ "ServerName" ) srv=$2 }; END { print srv }' sites.conf

I known awk command is powerfull, but little complex for beginner ^^.

Thanks for your help.

RE: Apache vhost - block treatment

Hi Frial,

I tried this

frial.sh

CODE

awk '$1 ~ /ServerName/ {
   server_name = $2 
}
$1 ~ /ProxyPass/ {
  where = $2
  line = "Vhost#"server_name where
  if (line != line_previous) {
    print line
  }
  line_previous = line
}' frial.conf 

and got the result

CODE

$ frial.sh
Vhost#site1.compagny.org/stat
Vhost#site2.compagny.org/office
Vhost#site2.compagny.org/home 

RE: Apache vhost - block treatment

(OP)
Hi mikrom,
Thank you very much for your help, that's perfect thumbsup2
I thought I would have to process the files by blocks, but that was not a good idea.

RE: Apache vhost - block treatment

Hi Frial,

awk processes the files column-wise. The default field separator is space, so it is as you would have in the file these 3 columns:



RE: Apache vhost - block treatment

Hi Frial,

As I looked at the table, which screenshot I posted before, I saw that the script can be greatly simplified:

CODE

awk '$1=="ServerName"{srv=$2} $1=="ProxyPass"{print "Vhost#"srv $2}' frial.conf 

RE: Apache vhost - block treatment

(OP)
Yes, it's more simple :)
When vhost doesn't contain "ProxyPass" value, when I read those scripts, I think output is empty.
I must call print function after set variables no?

RE: Apache vhost - block treatment

when between the tags <VirtualHost *:443> and </VirtualHost> there is no "ProxyPass" contained and you want to print in this case the server name, then you can do that after you read the line with the ending tag </VirtualHost> like this:

CODE

$1 == "ServerName"{
  srv = $2
  ProxyPass_found = 0
} 
$1 == "ProxyPass"{
  ProxyPass_found = 1
  print "Vhost#"srv $2
}
$1 == "</VirtualHost>" {
  if (!ProxyPass_found) {
     print "Vhost#"srv
  }
} 

RE: Apache vhost - block treatment

(OP)
Hi,

I hadn't thought of doing this so simply, thank you very much!
I added one more condition if vhost does'nt contain servername:

CODE -->

$1 == "ServerName"{
  srv = $2
  ProxyPass_found = 0
} 
$1 == "ProxyPass"{
  ProxyPass_found = 1
  print "Vhost#"srv $2
}
$1 == "</VirtualHost>" {
  if (!ProxyPass_found && srv) {
     print "Vhost#"srv
  }
} 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close