-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrir-delegated-latest-requests.py
executable file
·47 lines (40 loc) · 1.83 KB
/
rir-delegated-latest-requests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
from requests import get
from sys import argv
from re import search
from math import log2
"""
Programmed with requests.
Global RIR IPv4 CIDR prefix extractor, by country.
It now searches for a particular CC in all RIRs:
RIPE NCC, APNIC, ARIN, LACNIC and AFRINIC
Usage: ./program.py countrycode (optional: file)
If a file isn't an argument, it prints prefixes to stdout.
PEP8 compliant
"Explicit is better than implicit."
— The Zen of Python
"""
RIRs = ("https://ftp.lacnic.net/pub/stats/ripencc/delegated-ripencc-latest",
"https://ftp.lacnic.net/pub/stats/apnic/delegated-apnic-latest",
"https://ftp.lacnic.net/pub/stats/arin/delegated-arin-extended-latest",
"https://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest",
"https://ftp.lacnic.net/pub/stats/afrinic/delegated-afrinic-latest")
if len(argv) > 1:
for url in RIRs:
# reads content from URLs one by one
for prefix in get(url).text.split():
regex = search(str(argv[1]) + '.*ipv4', prefix)
if regex: # searches for cc and ipv4 strings
netaddr = prefix.split("|")[3] # net addr
bitmask = int(prefix.split("|")[4]) # bits used by net addr
cidrmask = int(32 - log2(bitmask)) # converts bits into CIDR
if len(argv) == 2:
print(f'{netaddr}/{cidrmask}') # prints to stdout
elif len(argv) == 3:
with open(f'{argv[2]}.txt', 'a') as file:
print(f'{netaddr}/{cidrmask}', file=file)
else:
print('Please provide at least a universal country code. (Optional: a\
filename descriptor to save the results.)\n\
Ex: ./program.py GB (print to stdout) OR ./program.py GB ipaddr-gb.txt \
(write to file "ipaddr-gb.txt" as an example)')