-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathipv6he_stats.py
More file actions
executable file
·61 lines (51 loc) · 1.63 KB
/
ipv6he_stats.py
File metadata and controls
executable file
·61 lines (51 loc) · 1.63 KB
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
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
### remove number 3 ^^^ from the end for python 2.x
import os
import json
import time
try:
# for python 3:
from urllib.request import urlopen
except ImportError:
# for python 2:
from urllib2 import urlopen
# ------------------------
feed_url = 'http://ipv6.he.net/exhaustionFeed.php?platform=json'
feed_cache = '/tmp/ipv6he_stats.cache'
cache_timeout = 1330 # minutes
# ------------------------
def print_rir(data, name):
print(" {0}: {1:,} - {2}%".format(
name,
int(data[name.lower()+'24s']),
data[name.lower()+'Percent']
))
def print_results(data):
print('--- IPv4/6 Statistics ---')
print('RIR v4 /24s Left:')
for rir in ('AfriNIC', 'APNIC', 'ARIN', 'LACNIC', 'RIPE'):
print_rir(data, rir)
print('ASNs: {0}/{1}\nGlues: {2}\nDomains: {3:,}\n0Day: {4}'.format(
data['v6ASNs'], data['totalASNs'],
data['v6NS'],
int(data['v6Domains']),
data['exhaustionDateIPv4Depletion']
))
def get_data(feed_url, feed_cache, cache_timeout):
cache_timeout = 60 * 10
data = None
try:
if ((os.stat(feed_cache).st_mtime + cache_timeout)
>= time.time()):
with open(feed_cache, 'r') as cache:
data = json.load(cache)
except (OSError, ValueError) as e:
pass
if not data:
feed_source = urlopen(feed_url).read().decode('utf-8')
with open(feed_cache, 'w') as cache:
cache.write(feed_source)
data = json.loads(feed_source)
return data
if __name__ == '__main__':
print_results(get_data(feed_url, feed_cache, cache_timeout))