Skip to content

Instantly share code, notes, and snippets.

@oskar456
Last active July 28, 2023 19:24
Show Gist options
  • Save oskar456/693fea6f4ecab060c739712f97eab7ee to your computer and use it in GitHub Desktop.
Save oskar456/693fea6f4ecab060c739712f97eab7ee to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import urllib.request
from collections import defaultdict
DELEGSTATS = "https://ftp.ripe.net/ripe/stats/delegated-ripencc-extended-latest"
def main():
"""
Count number of IP blocks per country.
"""
stats = {
"ipv4": defaultdict(int),
"ipv6": defaultdict(int),
"asn": defaultdict(int),
}
with urllib.request.urlopen(DELEGSTATS) as s:
for line in s:
if b"allocated" not in line:
continue
_, country, family, _, count, *_ = line.decode().split("|")
if family == "ipv6":
# For IPv6 we count number of /48s
stats[family][country] += pow(2, 48-int(count))
else:
stats[family][country] += int(count)
print("Country IPv4 /48s ASNs")
print(34*"=")
for country in sorted(stats["ipv4"],
key=lambda x: stats["ipv4"][x],
reverse=True):
print(f'{country:<5} {stats["ipv4"][country]:>10} '
f'{stats["ipv6"][country]:>10} {stats["asn"][country]:>6}'
)
if __name__ == '__main__':
main()
Country IPv4 /48s ASNs
==================================
DE 84348288 1547042816 2985
GB 78895360 1493303296 3030
FR 68490752 976683008 1926
IT 44087552 685244416 1287
RU 41206272 938344448 5869
ES 29623808 397803520 1212
NL 29266432 767295488 1515
SE 23370496 455606272 875
PL 18389248 406519808 2547
TR 16298752 244252672 869
IR 12145920 197525504 753
DK 10750720 110231552 454
SA 10290944 30146560 183
CH 9995520 255393792 1043
FI 9849856 96862208 370
BE 8894976 109445120 386
NO 8716800 149094400 374
AT 8441856 154271744 748
CZ 8185856 195624960 683
UA 7829760 171376640 2165
US 7828992 228065280 693
RO 7717888 84082688 1164
PT 6407936 38797312 143
IL 6057600 53542912 350
IE 5716480 69402624 291
HU 5266944 45547520 300
GR 4761600 30801920 226
AE 4701696 200409088 128
BG 4214784 74186752 768
KZ 3171328 24838144 190
LT 2464768 123142144 199
SI 2374912 30277632 293
HR 2304256 22872064 170
SK 2182400 39583744 222
RS 2070528 26083328 202
BY 1779200 9961472 121
KW 1681920 12320768 76
LV 1580800 22478848 307
EE 1264128 57344000 198
SY 1259264 9175040 5
GE 1254912 24510464 152
MD 1213440 96600064 215
CY 1181184 169279488 155
LU 932864 29556736 129
OM 890112 7340032 22
PS 856576 13107200 67
QA 843008 4653056 20
BA 771584 10616832 54
AZ 755200 22937600 81
MK 684288 13565952 62
MT 670464 9043968 54
IS 669696 17301504 90
JO 643840 13238272 50
AM 619520 17629184 120
LB 586752 37945344 173
AL 470528 35520512 108
BH 451072 5111808 28
IQ 445696 52363264 205
HK 306176 38469632 85
CA 268800 9240576 82
SC 265984 132448256 62
GI 244480 5177344 33
UZ 242176 12713984 93
KG 239360 5832704 59
ME 204544 6553600 27
YE 139264 3145728 6
AU 134656 5570560 30
IN 132096 33095680 39
MQ 131328 1245184 5
AG 113920 720896 6
GP 107520 524288 2
LI 88832 5505024 26
JE 77568 3407872 13
IM 74752 4980736 23
TJ 68352 4390912 28
MC 61952 1703936 3
BZ 56320 4653056 17
AD 56320 131072 3
RE 52224 589824 2
VG 51200 6422528 28
CN 49408 3670016 187
FO 44032 1638400 6
SM 36096 2359296 11
GG 36096 1703936 7
GL 34304 655360 1
PA 27648 6946816 13
TM 21760 524288 6
SG 13056 2621440 17
CW 12288 1572864 6
JP 10752 2097152 17
VA 10240 1114112 3
ZA 9472 1245184 7
NZ 9216 3211264 6
FK 7168 0 2
NG 5120 524288 2
BM 4352 131072 2
AX 4096 65536 1
LY 3072 524288 0
TW 2048 524288 56
BD 2048 524288 2
BR 2048 524288 12
VC 2048 65536 1
VN 1536 524288 7
AF 1536 0 0
PK 1280 0 2
DM 1024 524288 2
KY 1024 0 1
TN 1024 0 2
EG 1024 0 4
AO 1024 524288 0
VU 1024 524288 1
KE 256 524288 3
KR 256 524288 18
BQ 256 524288 1
TH 256 524288 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment