Skip to content

Instantly share code, notes, and snippets.

@mrjones-plip
Last active June 25, 2021 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrjones-plip/f60d09071e043baa1384143d32955f37 to your computer and use it in GitHub Desktop.
Save mrjones-plip/f60d09071e043baa1384143d32955f37 to your computer and use it in GitHub Desktop.
whois lookup python script
import time
import whois
import whois.parser
import argparse
import logging.handlers
import sys
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler(address='/dev/log')
my_logger.addHandler(handler)
parser = argparse.ArgumentParser()
parser.add_argument('--character', '-c', type=str, help='Single character to expand - required', required=True)
parser.add_argument('--pre', '-pr', default='', type=str, help='Prefix of domain - defaults to NULL', required=False)
parser.add_argument('--post', '-po', default='', type=str, help='Postfix of domain - defaults to NULL', required=False)
parser.add_argument('--tld', '-t', default='com', type=str,
help='Top level domain (TLD) of domain - with out leading "." - defaults to ".com"', required=False)
args = parser.parse_args()
tld = args.tld
pre = args.pre
post = args.post
char = args.character
def full_stack():
import traceback, sys
exc = sys.exc_info()[0]
stack = traceback.extract_stack()[:-1] # last one would be full_stack()
if exc is not None: # i.e. an exception is present
del stack[-1] # remove call of full_stack, the printed exception
# will contain the caught exception caller instead
trc = 'Traceback (most recent call last):\n'
stackstr = trc + ''.join(traceback.format_list(stack))
if exc is not None:
stackstr += ' ' + traceback.format_exc().lstrip(trc)
return stackstr
def whois_domain(domain, iteration=1):
my_logger.debug('DOMAINS: ' + domain)
try:
result = whois.query(domain=domain, cache_file='./cache.json', slow_down=2)
return result
except whois.exceptions.WhoisCommandFailed as e:
print(domain, ': WhoisCommandFailed error, sleeping 5 (iteration ', str(iteration), ')')
# print('Error: ', e)
my_logger.debug('DOMAINS: WhoisCommandFailed, sleeping 5 (iteration ' + str(iteration) + ')')
time.sleep(5)
if iteration == 3:
return False
else:
iteration += 1
return whois_domain(domain, iteration)
except whois.parser.PywhoisError:
return False
def iterate_whois(pre, char, post, tld):
originalChar = char
domain = pre + char + post + '.' + tld
while len(domain) < 68:
w = whois_domain(domain)
if w is not False and hasattr(w, 'creation_date'):
if isinstance(w.creation_date,list):
print(domain, ': ', w.creation_date[0])
else:
print(domain, ': ', w.creation_date)
# print(vars(w))
else:
print(domain, ': NA')
char += originalChar
domain = pre + char + post + '.' + tld
if __name__ == "__main__":
try:
my_logger.debug('DOMAINS: Starting')
iterate_whois(pre, char, post, tld)
print('Done!')
pass
except KeyboardInterrupt:
my_logger.debug("DOMAINS: Stopping(Ctrl + C)")
pass
finally:
my_logger.debug("DOMAINS exit trace: " + full_stack())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment