Created
May 31, 2023 20:41
-
-
Save edsu/bb65e822b381678d4764e4948ee234cb to your computer and use it in GitHub Desktop.
This file contains 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
import csv | |
import time | |
from ipwhois import IPWhois | |
output = csv.DictWriter(open("blocks.csv", "w"), ["ip", "affected", "name", "country", "description"]) | |
output.writeheader() | |
for line in open("blocks.txt"): | |
line = line.strip() | |
row = {"ip": line} | |
parts = line.split("/") | |
if len(parts) == 1: | |
ip = parts[0] | |
row["affected"] = 1 | |
else: | |
ip, cidr = parts | |
cidr = int(cidr) | |
row["affected"] = 2 ** (32 - cidr) | |
addr = IPWhois(ip) | |
result = addr.lookup_rdap(depth=1) | |
if result is None or 'network' not in result: | |
output.writerow(row) | |
continue | |
row["name"] = result["network"]["name"] | |
row["country"] = result["network"]["country"] | |
if result['network']['remarks'] and len(result['network']['remarks']) > 0: | |
row["description"] = result['network']['remarks'][0]['description'].replace("\n", " ") | |
output.writerow(row) | |
time.sleep(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment