Last active
February 2, 2016 13:28
-
-
Save hensapir/74ef2e8492834437a407 to your computer and use it in GitHub Desktop.
Apache VirtualHost redirect test -- tests if a web server redirects to all URLs specified in an Apache .conf file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python2 | |
import re | |
import sys | |
import os.path | |
import requests | |
def get_all_server_names(vhost_filename): | |
""" | |
Returns a list of all server names specified in vhost_file parameter | |
Keyword arguments: | |
vhost_filename -- full path to virtual host file | |
""" | |
if not os.path.isfile(vhost_filename): | |
sys.exit('Unknown file %s' % vhost_filename) | |
f = open(vhost_filename, 'r') | |
data_all = f.readlines() | |
f.close() | |
data = filter( lambda i: re.search('^((?!#).)*$', i), data_all) | |
vhost_id = 0 | |
enable = False # flag to set start/end marker for vhost brackets | |
results = {} | |
curr_vhost = [] # stores [ip, port] of current vhost | |
server_names = [] # stores all parsed server names | |
# while there are still vhosts to parse | |
while len(data) > 0: | |
out = data.pop(0) | |
# start of VirtualHost | |
if "<VirtualHost" in out: | |
ip, port = out.split()[1].split(':') | |
port = port.replace('>', '') | |
curr_vhost.append(ip) | |
curr_vhost.append(port) | |
enable = True | |
continue | |
if "</VirtualHost>" in out: | |
results[vhost_id] = curr_vhost | |
vhost_id += 1 | |
enable = False | |
curr_vhost = [] | |
continue | |
if enable: | |
curr_vhost.append(out) | |
continue | |
for i in results: | |
for line in results[i]: | |
if "ServerName" in line: | |
servername = line.split()[1] | |
if not servername.startswith('http'): | |
servername = '%s%s' % ('http://', servername) | |
continue | |
server_names.append(servername) | |
return server_names | |
def add_to_hosts(servername): | |
""" | |
Adds servername to /etc/hosts pointing to localhost | |
Keyword arguments: | |
servername -- URL of vhost redirect server | |
""" | |
with open('/etc/hosts', 'a') as f: | |
f.write('127.0.0.1 %s \n' % servername) | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
sys.exit('Usage: %s FILE' % sys.argv[0]) | |
FILE = sys.argv[1] | |
# Make temporary hosts file and store old contents in it | |
with open('/etc/hosts' + "_temp", "w") as t: | |
with open('/etc/hosts', 'r') as f: | |
t.write(f.read()) | |
servernames = get_all_server_names(FILE) | |
for server in servernames: | |
add_to_hosts(server) | |
fail_count = 0 | |
for server in servernames: | |
status_code = requests.get(server, allow_redirects=False).status_code | |
# check if status_code is not a redirect | |
if status_code >= 400 or status_code < 300: | |
print("FAIL: %s status_code: %s" % (server, status_code)) | |
fail_count = fail_count + 1 | |
print("Out of {0} tests run: {1} tests passed, {2} tests failed.".format(len(servernames), len(servernames) - fail_count, fail_count)) | |
# overwrite the hosts file with its original contents | |
with open('/etc/hosts', "w") as f: | |
with open('/etc/hosts' + "_temp", 'r') as t: | |
f.write(t.read()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment