Unix Shell - Retrieve VirtualHost matching ServerName

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2006 > Retrieve VirtualHost matching ServerName





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Retrieve VirtualHost matching ServerName
Davyp

2006-11-23, 7:33 am

Hi I want to retrive a VirtualHost directive from an apache httpd.conf
file based on a search of the ServerName.

So ideally I get the following when I search for domainName.com.

<VirtualHost *>
ServerAdmin webmaster@domainName.com
DocumentRoot "/var/www/vhosts/domainName.com"
ServerName domainName.com
ErrorLog /var/www/logs/domainName.com/error_logs/
CustomLog /var/www/logs/domainName.com/access_log "combined"
</VirtualHost>


awk '/ServerName.*domainName.com/,/VirtualHost/' httpd.conf
gives me:
ServerName domainName.com
ErrorLog /var/www/logs/domainName.com/error_logs/
CustomLog /var/www/logs/domainName.com/access_log "combined"
</VirtualHost>

Any ideas on how to search backward to the previous occurrence of
<VirtualHost and return that too?

awk '/VirtualHost/,/ServerName.*domainName.com/' httpd.conf

seems to output the whole file minus the lines between "ServerName
domainName.com" and the following "</VirtualHost>".

Davyp

2006-11-24, 7:24 am

Davyp wrote:

> Hi I want to retrive a VirtualHost directive from an apache httpd.conf
> file based on a search of the ServerName.
>
> So ideally I get the following when I search for domainName.com.
>
> <VirtualHost *>
> ServerAdmin webmaster@domainName.com
> DocumentRoot "/var/www/vhosts/domainName.com"
> ServerName domainName.com
> ErrorLog /var/www/logs/domainName.com/error_logs/
> CustomLog /var/www/logs/domainName.com/access_log "combined"
> </VirtualHost>
>
>
> awk '/ServerName.*domainName.com/,/VirtualHost/' httpd.conf
> gives me:
> ServerName domainName.com
> ErrorLog /var/www/logs/domainName.com/error_logs/
> CustomLog /var/www/logs/domainName.com/access_log "combined"
> </VirtualHost>
>
> Any ideas on how to search backward to the previous occurrence of
> <VirtualHost and return that too?
>
> awk '/VirtualHost/,/ServerName.*domainName.com/' httpd.conf
>
> seems to output the whole file minus the lines between "ServerName
> domainName.com" and the following "</VirtualHost>".


Well I found a solution which works though it is pretty horrible:

FILENAME=httpd.conf
SERVERNAME=whatever.com
STOPMATCH=VirtualHost

# Finds the line number of the ServerName search occurance in the the
# httpd.conf filename.
LINENUMBER=`grep -n -e "${SERVERNAME}" ${FILENAME} | sed "s/:.*//"`

# TEST will equal the empty string until $LINENUMER is one on which
# there is the string VirtualHost.
TEST=`awk "NR==${LINENUMBER}" ${FILENAME} | grep "${STOPMATCH}"`

REVERSELINE=()
ARRAYPOINTER=0

# Until the line at $LINENUMBER contains the string VirtualHost add
# it to the REVERSELINE array and minus one from the line count.
until [ "${TEST}" != "" ]
do
let LINENUMBER=${LINENUMBER}-1
REVERSELINE[${ARRAYPOINTER}]=`awk "NR==${LINENUMBER}" ${FILENAME}`
let ARRAYPOINTER=${ARRAYPOINTER}+1
TEST=`awk "NR==${LINENUMBER}" ${FILENAME} | grep "${STOPMATCH}"`
done

ARRAYSIZE=${#REVERSELINE[*]}

# Print the array out in reverse order
while [ "$ARRAYSIZE" -gt "0" ]
do
echo ${REVERSELINE[$ARRAYSIZE -1]}
let ARRAYSIZE=${ARRAYSIZE}-1
done

# Print the rest of the section from the ServerName to the next
# VirtualHost match.
awk "/${SERVERNAME}/,/${STOPMATCH}/" ${FILENAME}

Radoulov, Dimitre

2006-11-25, 7:21 am


"Davyp" <davidprangnell@gmail.com> wrote in message
news:1164279830.414764.49990@e3g2000cwe.googlegroups.com...
> Hi I want to retrive a VirtualHost directive from an apache httpd.conf
> file based on a search of the ServerName.
>
> So ideally I get the following when I search for domainName.com.
>
> <VirtualHost *>
> ServerAdmin webmaster@domainName.com
> DocumentRoot "/var/www/vhosts/domainName.com"
> ServerName domainName.com
> ErrorLog /var/www/logs/domainName.com/error_logs/
> CustomLog /var/www/logs/domainName.com/access_log "combined"
> </VirtualHost>

[...]

This __may__ work with certain httpd.conf formats:

gawk 'BEGIN{RS="<VirtualHost"}
/domainName.com/{sub(/VirtualHost>.*/,"VirtualHost>")
print RS$0}' httpd.conf


Regards
Dimitre


Davyp

2006-11-29, 7:28 am


Radoulov, Dimitre wrote:

> "Davyp" <davidprangnell@gmail.com> wrote in message
> news:1164279830.414764.49990@e3g2000cwe.googlegroups.com...
> [...]
>
> This __may__ work with certain httpd.conf formats:
>
> gawk 'BEGIN{RS="<VirtualHost"}
> /domainName.com/{sub(/VirtualHost>.*/,"VirtualHost>")
> print RS$0}' httpd.conf
>
>
> Regards
> Dimitre


Thanks a lot! I'll give it a go

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com