Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
81 lines (67 sloc) 2.4 KB
import subprocess
import multiprocessing
import logging
import time
import indexCreation
import signal
from jsonSending import flatJsonEl, sendToElastic, stringToJson
START_DELAY = 30
SLEEP_TIME = 10
CALL_BACK_CALLED = False
def callBackFunc(t):
global CALL_BACK_CALLED
CALL_BACK_CALLED = True
def procFunc(arg_tuple):
if arg_tuple[0].replace('\n', '') == '':
return
try:
signal.signal(signal.SIGCHLD, signal.SIG_IGN)
url, elastic_index = arg_tuple[0], arg_tuple[1]
args = ' '.join(["./ssllabs-scan --ignore-mismatch=true", url.replace('\n', '')])
ans = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE)
logging.info('Scan started ' + url)
ans.wait(600)
except subprocess.TimeoutExpired:
ans.poll()
ans.kill()
logging.error('Killed proc with args ' + args)
except:
logging.error('Could not start a proc with args ' + args)
else:
data = ans.communicate()
string_data = data[0].decode('utf-8')
json_el = stringToJson(string_data)
if json_el is None:
return
logging.info(url + ' Scanning complete, sending to elastic')
sendToElastic(index_name='hosts', index_counter=elastic_index, json_data=flatJsonEl(json_el, elastic_index))
logging.info('sent ' + str(elastic_index) + ' URL: ' + url)
ans.kill()
finally:
return
def main(path_to_domains='/usr/share/all_domains',proc_count = 3):
args_list = []
p = multiprocessing.Pool(processes=proc_count, maxtasksperchild=1)
with open(path_to_domains, 'r') as fp:
urls = fp.readlines()
for index, url in enumerate(urls):
args_list.append((url, index + 1))
try:
p.map_async(procFunc, args_list, chunksize=1, callback=callBackFunc)
start_time = time.time()
while time.time() - start_time < 36000:
time.sleep(SLEEP_TIME)
if CALL_BACK_CALLED:
p.terminate()
return
except:
logging.error('Could not start threads')
if __name__ == '__main__':
time.sleep(START_DELAY)
logging.basicConfig(filename='/var/log/ssllabs-scan.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
indexCreation.main()
while True:
main(proc_count=5)
global CALL_BACK_CALLED
CALL_BACK_CALLED = False
logging.info('ALL DONE')