-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrir-delegated-latest-urllib.request.py
executable file
·52 lines (45 loc) · 2.02 KB
/
rir-delegated-latest-urllib.request.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
48
49
50
51
52
#!/usr/bin/env python3
from sys import argv
from re import search
from math import log2
from urllib.request import urlopen
import ssl
"""
Programmed with urllib.
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
"""
""" Bypass SSL/TLS checks """
ct = ssl.create_default_context()
ct.check_hostname = False
ct.verify_mode = ssl.CERT_NONE
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:
for prefix in urlopen(url, context=ct).read().\
decode('utf-8').split(): # reads content from URLs one by one
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)')