Find All Virtual Hosts a Web Server is Serving
I registered a couple domain names recently and it was too hard to manually keep track of where everything was in the process of getting set up, so I wrote a few scripts to help with the job. They’re all on Github, which you already know, if you’re one of my zero followers. The one I’m highlighting here finds all the virtual hosts on a given system.
First off, I want to know what all I’ve got set up to be served. It trolls /etc/apache’s vhosts or sites-enabled directory, but it would work just as well with nginx. It’s basically one line. I hit Google hard for this one:
egrep -rh "ServerAlias|ServerName" $base/*.conf $exclude_dir | grep -v '#' | sed -e 's/ServerName //g' | sed -e 's/ServerAlias //g' | sed -e 's/*.//g' | awk '{$1=$1};1' | tr " " "\n"
- Check the given directory for all instances of ServerName or ServerAlias.
- Exclude those that are commented out
- Trim the word ServerName or ServerAlias off so the end result is a list of domain names.
- If there’s a wildcard alias, trim off the wildcard portion. This will result in apparent duplicates.
- awk removes leading whitespace
- tr changes spaces into newlines
I tried piping the end result through uniq, but it wasn’t sticking. I assume that has to do with how tr works and that xargs would save the day. Once that’s sorted out, I’ll sort the results alphabetically.