Last active
November 27, 2022 08:58
-
-
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
This file contains hidden or 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 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