Skip to content

Instantly share code, notes, and snippets.

@mrhalix
Last active November 27, 2022 08:58
Show Gist options
  • Save mrhalix/f07cf40144dfc29d71aec3de0a47afee to your computer and use it in GitHub Desktop.
Save mrhalix/f07cf40144dfc29d71aec3de0a47afee to your computer and use it in GitHub Desktop.
Parse txt file from https://ftp.ripe.net/ripe/stats/membership/alloclist.txt , and optionally export it as csv
import requests
import re
alloclist = requests.get("https://ftp.ripe.net/ripe/stats/membership/alloclist.txt")
def splitter(text):
splitted = {}
current_company = []
for i in text.split("\n"):
if i == '': continue
if re.match("([A-z][A-z])\.(.*)", i): # means processing a new block
current_company = splitted[i] = []
continue
current_company.append(i.strip())
return splitted
splitted = splitter(alloclist.text)
# export csv
csv = ""
for company in splitted:
for ip in splitted[company][1:]: # removing ASN full name
ip = ip.split("\t")
company_ = company.split(".")
csv += company_[0] + "," + company_[1] + "," + splitted[company][0] + "," + ip[0] + "," + ip[1] + "\n"
print(csv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment