Created
March 7, 2018 09:58
-
-
Save nbeernink/8581fade98576bb78fc0446e4edebc56 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 python3 | |
| import argparse | |
| import errno | |
| import os.path | |
| import requests | |
| import sys | |
| from termcolor import colored | |
| parser = argparse.ArgumentParser(description='Check http status code for a list of domains.') | |
| parser.add_argument('-d', '--domain-list', dest='domains_list', help='The list of domains list', required=True) | |
| parser.add_argument('-o', '--output', dest='outputfile', help='The optional status output file', required=True) | |
| args = parser.parse_args() | |
| if not os.path.isfile(args.domains_list): | |
| raise FileNotFoundError( | |
| errno.ENOENT, os.strerror(errno.ENOENT), args.domains_list) | |
| else: | |
| summary = [] | |
| header = 'Fetching status for domains from ' + os.path.abspath(args.domains_list) + '...' | |
| print(header) | |
| # print an underline equal to the length of the header | |
| print('=' * int(len(header))) | |
| with open(args.domains_list, 'r') as file, open(args.outputfile, 'w') as output: | |
| for domain in file: | |
| domain = domain.strip('\n') | |
| try: | |
| r = requests.get('http://' + domain,allow_redirects=False,timeout=3) | |
| domain = domain + ':' | |
| status = str(r.status_code) | |
| output.writelines(domain + status + '\n') | |
| summary.append(status) | |
| if int(status) == 200: | |
| print(domain + colored(status, 'green')) | |
| elif int(status) >= 500: | |
| print(domain + colored(status, 'red')) | |
| else: | |
| print(domain + colored(status, 'yellow')) | |
| except requests.Timeout: | |
| print(domain + colored(' took too long to respond', 'red')) | |
| except requests.ConnectionError: | |
| print(domain + colored(' failed to connect', 'red')) | |
| print('\nFinished!\n') | |
| summary_footer = 'Summary for ' + str(len(summary)) + ' domains:' | |
| print(summary_footer) | |
| # print an underline equal to the length of the summary_footer | |
| print('=' * int(len(summary_footer))) | |
| for statuscode in set(summary): | |
| print(statuscode + ': ' + str(summary.count(statuscode))) | |
| print('output saved to: ' + os.path.abspath(args.outputfile)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment