Skip to content

Instantly share code, notes, and snippets.

@rootshellz
Created May 8, 2019 01:44
Show Gist options
  • Save rootshellz/48498d31fe6f998c25aa926df408a2b7 to your computer and use it in GitHub Desktop.
Save rootshellz/48498d31fe6f998c25aa926df408a2b7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import json
from cymruwhois import Client
if len(sys.argv) != 3:
sys.exit("Usage: python3 {} <input_file> <output_file>\n\
input_file should be a text file containing a unique list of IPs\n\
output_file will be a json formatted file of results".format(sys.argv[0]))
cy = Client()
ips = []
results = {}
with open(sys.argv[1], "r") as fh:
for line in fh.readlines():
ips.append(line.strip())
for ip in ips:
result = cy.lookup(ip)
if result.prefix in results:
results[result.prefix]["ips"].append(result.ip)
results[result.prefix]["count"] += 1
else:
results[result.prefix] = {
"ips": [result.ip,],
"count": 1,
"asn": result.asn,
"country": result.cc,
"owner": result.owner
}
with open(sys.argv[2], "w+") as fh:
fh.write(json.dumps(results, indent=4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment