Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Apache vhost - block treatment

Status
Not open for further replies.

Frial

Systems Engineer
Jun 16, 2022
4
FR
Hi all,

I'm a beginner's awk.
I have a file apache like this:
Code:
<VirtualHost *:443>
        ServerName site1.compagny.org
        DocumentRoot /var/[URL unfurl="true"]www/html/[/URL]

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

        ProxyPass /stat [URL unfurl="true"]https://srv.compagny.org/stat[/URL]
        ProxyPassReverse /stat [URL unfurl="true"]https://srv.compagny.org/stat[/URL]
</VirtualHost>

<VirtualHost *:443>
        ServerName site2.compagny.org
        DocumentRoot /var/[URL unfurl="true"]www/html/[/URL]

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

        ProxyPass /office [URL unfurl="true"]https://srv2.compagny.org/office[/URL]
        ProxyPassReverse /office [URL unfurl="true"]https://srv2.compagny.org/office[/URL]
        ProxyPass /home [URL unfurl="true"]https://srv2.compagny.org/home[/URL]
        ProxyPassReverse /home [URL unfurl="true"]https://srv2.compagny.org/home[/URL]
</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.
 
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
 
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.
 
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:

awk_frial_pebpvs.png
 
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
 
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?

 
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
  }
}
 
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
  }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top