19 November 2009 - 20:13find out if your Apache is really serving the right hosts
From time to time it can happen that websites that used to be on your webservers are being moved by your clients without notifying you. Therefore, you will have to search for «zombie websites».
I have the following setup: There is an Apache webserver that has a lot of websites. Each website has at least two Virtual Hosts, a first for the main website and a second one fetching all the aliases and redirecting them to the first. Each website has its own config file.
So, if I want to know whether a website is still being served by my machine, I run the following:
for host in `grep "Server\(Alias\|Name\)" _config/apache/*
| awk '{ for(i=3 ; i<=NF ; i++)printf "%s\n", $i}'`; do echo
-n `nslookup "$host" | grep -A1 Name | grep Address | awk
'{print $2}'`; echo " $host"; done
Okay, let’s go through it step by step:
for host in `grep “Server\(Alias\|Name\)” _config/apache/*
Here we extract all lines containing ServerAlias or ServerName in all config files. The output will look like
_config/apache/foo.bar.conf: ServerName www.foo.bar
_config/apache/foo.bar.conf: ServerAlias foo.bar fuh.bar www.foo.bar _config/apache/example.com.conf: ServerName www.example.com _config/apache/yah.conf: ServerAlias example.com
As we can see, the results have a varying size. Therefore, we have to tell awk about it.
| awk ‘{ for(i=3 ; i<=NF ; i++)printf “%s\n”, $i}’`
Awk takes each line and starts to read it from the third column (each seperated with a space) until EOL. What we get is being printed with a CRLF so that we do not get example.com fuh.bar www.foo.bar but
example.com fuh.bar www.foo.bar
do echo -n
Now we print each of the results of the following command without a CRLF afterwards.
`nslookup “$host” | grep -A1 Name | grep Address | awk ‘{print $2}’`;
Here we perform a IP lookup of the current host, grep for a string called “Name” and let it print the following line as well as it contains the IP address we are looking for. As we just need the IP address, we do another grep on the result and pick the second line. But we don’t want the “Address” string here, so we awk it away. To make it a little bit easier to understand, I will quickly show you what these commands do:
nslookup example.com Server: 213.133.100.100 Address: 213.133.100.100#53 Non-authoritative answer: Name: example.com Address: 192.0.32.10 --- nslookup example.com | grep -A1 Name Name: example.com Address: 192.0.32.10
---
nslookup example.com | grep -A1 Name | grep Address Address: 192.0.32.10
---
nslookup example.com | grep -A1 Name | grep Address | awk '{print $2}'
192.0.32.10
echo ” $host”
Now we have an IP but we would like to know what the corresponding hostname is, so we quickly echo it.
Done. Afterwards we can see:
192.0.32.10 www.example.com
yetanotherhost.mil
No Comments
Categories: Linux, Webserver, howto
Tags: apache, awk, awk example, bashscript, code, find ip of apache host, find ip of vhost, find vhosts, grep, grep example, howto, Linux, nslookup, server, Software, vhost, Webserver