Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from pygments import highlight, lexers, formatters
- import os
- import itertools
- import pygeoip
- import json
- import dns.resolver
- import tqdm
- import signal
- import time
- import sys
- import multiprocessing
- signal.signal(signal.SIGPIPE, signal.SIG_DFL) #IOError: Broken Pipe
- signal.signal(signal.SIGINT, signal.SIG_DFL) #KeyboardInterrupt: Ctrl-C
- def get_host_ip(output):
- my_resolver = dns.resolver.Resolver()
- my_resolver.timeout = 3
- my_resolver.lifetime = 3
- try:
- answer = my_resolver.query(output, 'A')
- for rdata in answer:
- return rdata
- except:
- pass
- def remove_blanks(output):
- if output:
- text = os.linesep.join([s for s in output.splitlines() if s])
- return text
- def ipLocator(ip):
- ip = remove_blanks(ip)
- try:
- LIST_OF_GEO = []
- GeoIPDatabase = './GeoLiteCity.dat'
- ipData = pygeoip.GeoIP(GeoIPDatabase)
- record = ipData.record_by_name(ip)
- if record:
- record['Hostname'] = ip
- record['IP-Addr'] = str(get_host_ip(ip))
- #print_this(record)
- LIST_OF_GEO.append(record.copy())
- if LIST_OF_GEO:
- return LIST_OF_GEO
- except:
- pass
- def print_this(record):
- formatted_json = json.dumps(record, sort_keys=True, ensure_ascii=True, indent=3)
- colorful_json = highlight(unicode(formatted_json, 'UTF-8'), lexers.JsonLexer(), formatters.TerminalFormatter())
- print(colorful_json)
- def main():
- #with open('netflix.txt', 'r') as f:
- with open(sys.argv[1], 'r') as f:
- MCKIN_HOSTS = [ l for l in f.readlines() if l ]
- if MCKIN_HOSTS:
- p = multiprocessing.Pool(25)
- result = p.map(ipLocator, MCKIN_HOSTS)
- if result:
- #if LIST_OF_GEO:
- print_this(result)
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment