Created
October 14, 2020 18:25
-
-
Save walkure/d1d87d8b4aad3c692edef1cce0f69aab to your computer and use it in GitHub Desktop.
指定地域のIPアドレス割り振り・割り当て一覧を作るスクリプト
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
from netaddr import IPSet, IPRange, IPAddress | |
import requests | |
import time | |
rirs = ( | |
'http://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest', | |
'http://ftp.ripe.net/pub/stats/ripencc/delegated-ripencc-extended-latest', | |
'http://ftp.apnic.net/pub/stats/apnic/delegated-apnic-extended-latest', | |
'http://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-extended-latest', | |
'http://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-extended-latest', | |
) | |
def loadDelegate(uri: str, cc: str) -> (list, list): | |
res = requests.get(uri) | |
if res.status_code != 200: | |
return None | |
v4list = list() | |
v6list = list() | |
# https://www.apnic.net/about-apnic/corporate-documents/documents/resource-guidelines/rir-statistics-exchange-format/#RecordFormat | |
for line in res.text.splitlines(): | |
if line.startswith('#'): | |
continue | |
params = line.split('|') | |
if params[1] != cc: | |
continue | |
if params[2] == 'ipv4': | |
width = int(params[4]) | |
from_ip = IPAddress(params[3], version=4) | |
to_ip = IPAddress(int(from_ip) + width - 1) | |
v4list.extend(IPRange(params[3], to_ip).cidrs()) | |
elif params[2] == 'ipv6': | |
v6list.append(params[3]+'/'+params[4]) | |
return (v4list, v6list) | |
if __name__ == '__main__': | |
start = time.time() | |
cc = 'JP' | |
v4list = list() | |
v6list = list() | |
for rir in rirs: | |
(v4, v6) = loadDelegate(rir, cc) | |
v4list.extend(v4) | |
v6list.extend(v6) | |
v4set = IPSet(v4list) | |
v6set = IPSet(v6list) | |
print("Elapsed {0} secs".format(time.time() - start)) | |
with open('./v4{}.txt'.format(cc), mode='w') as f: | |
for cidr in v4set.iter_cidrs(): | |
f.write(str(cidr)+"\n") | |
with open('./v6{}.txt'.format(cc), mode='w') as f: | |
for cidr in v6set.iter_cidrs(): | |
f.write(str(cidr)+"\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment