diff --git a/inspect-domains/files/inspect-domains.py b/inspect-domains/files/inspect-domains.py index e1120994..8512b04c 100755 --- a/inspect-domains/files/inspect-domains.py +++ b/inspect-domains/files/inspect-domains.py @@ -17,6 +17,7 @@ import subprocess import threading import time import argparse +import json #import importlib.machinery #list_domains = importlib.machinery.SourceFileLoader('list_domains.py', list_domains_path).load_module() @@ -40,7 +41,7 @@ def get_my_ips(): stdout, stderr = execute('hostname -I') if not stdout: return [] - return stdout[0].strip().split() + return stdout[0].strip(' \t').split() def dig(domain): @@ -48,6 +49,12 @@ def dig(domain): stdout, stderr = execute('dig +short {}'.format(domain)) return stdout + +def strip_comments(string): + """Return string with any # comment removed.""" + return string.split('#')[0] + + def list_apache_domains(): """Return a dict containing : - key: Apache domain (from command "apache2ctl -D DUMP_VHOSTS"). @@ -56,24 +63,24 @@ def list_apache_domains(): domains = {} try: - stdout, stderr = execute("apache2ctl -D DUMP_VHOSTS") + stdout, stderr = execute('apache2ctl -D DUMP_VHOSTS') except: - # Apache is not on the server + # Apache is not present on the server return domains - vhost_infos = "" + vhost_infos = '' for line in stdout: - dom = "" - words = line.strip().split() + dom = '' + words = line.strip(' \t').split() - if "namevhost" in line and len(words) >= 5: + if 'namevhost' in line and len(words) >= 5: # line format: port namevhost (:) - dom = words[3] - vhost_infos = "apache:" + words[4].strip('()') + dom = words[3].strip() + vhost_infos = 'apache:' + words[4].strip('()') - elif "alias" in line and len(words) >= 2: + elif 'alias' in line and len(words) >= 2: # line format: alias - dom = words[1] # vhost_infos defined in previous lines + dom = words[1].strip() # vhost_infos defined in previous lines if dom: if dom not in domains: @@ -84,6 +91,54 @@ def list_apache_domains(): return domains +def list_nginx_domains(): + """Return a dict containing : + - key: Nginx domain (from command "nginx -T"). + - value: a list of strings "nginx::" + """ + domains = {} + + try: + stdout, stderr = execute('nginx -T') + except: + # Nginx is not present on the server + return domains + + line_number = 1 + config_file_path = '' + + for line in stdout: + if '# configuration file' in line: + # line format : # configuration file : + words = line.strip(' \t;').split() + config_file_path = words[3].strip(' :') + continue + + if 'server_name ' in line: + # TODO: améliorer le if (cas tabulation) + # line format : server_name [