Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Check the response status code of all enabled vhosts.
Allowed status code are: 403 and 200.
'''
import os
import re
import requests
import sys
nginx_dir = '/etc/nginx/sites-enabled'
if not os.path.exists(nginx_dir):
print('Nginx dir does not exists ("%s").' % nginx_dir)
else:
requests.packages.urllib3.disable_warnings()
found = False
errors = 0
for name in os.listdir(nginx_dir):
path = os.path.join(nginx_dir, name)
with open(path, 'r') as fo:
conf = fo.read()
conf = conf.replace('\t', ' ')
matching = re.search(r'.*server_name\ +([0-9a-zA-Z\.\-\_\ ]+);.*', conf)
if not matching:
print('The server_name was not found in: "%s".' % path)
errors += 1
continue
domains = matching.groups()[0].strip().split(' ')
https = re.search(r'listen +\w* +ssl;', conf) is not None \
or re.search(r'ssl +on;', conf) is not None
for domain in domains:
if domain == 'localhost':
continue # status vhost
found = True
url = '%s://%s' % ('https' if https else 'http', domain)
sys.stdout.write('Testing url "%s": ' % url)
try:
req = requests.get(url, verify=False, timeout=5)
except Exception as e:
code = str(e)
else:
code = req.status_code
if code == 200 or code == 403:
sys.stdout.write('\033[92mOK (%s).\033[0m\n' % code)
else:
sys.stdout.write('\033[91mKO (%s).\033[0m\n' % code)
errors += 1
if errors:
print('%s vhost(s) did not correctly responded.' % errors)
sys.exit(1)
if not found:
print('No vhost found in Nginx sites-enabled dir.')
sys.exit(1)