-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.py
executable file
·78 lines (69 loc) · 2.02 KB
/
filter.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
import sys
import json
debug=False
""" input file format:
{
"status": "ok",
(...)
"data": {
"resource": "AT",
"stats": {
"total": 365
},
"probes": [
{
"prefix_v4": "193.238.156.0/22",
"status": 1,
"prefix_v6": "2a02:60::/29",
"is_anchor": false,
"last_connected": 1566920183,
"status_name": "Connected",
"type": "Probe",
"id": 33,
"tags": [
"cable",
"home",
"nat",
"upc",
"system-v1",
"system-resolves-a-correctly",
"system-resolves-aaaa-correctly",
"system-ipv4-works",
"system-ipv6-doesnt-work",
"system-ipv4-capable",
"system-ipv6-capable",
"system-ipv4-rfc1918",
"system-ipv4-stable-1d"
],
"address_v6": "2a02:61:bcb:1:220:4aff:fec8:22c2",
"longitude": 16.3585,
"address_v4": "193.238.159.251",
"""
def parsejson(j):
""" returns a tuple: (ipv4, ipv6, is_nat) or None if not OK."""
if j['status'] != 'ok':
return None
probes = j['data']['probes']
for probe in probes:
if debug:
print(probe)
tags = probe['tags']
if (probe['address_v4']):
if 'nat' in tags:
is_nat = True
else:
is_nat = False
ipv4 = probe['address_v4']
ipv6 = probe['address_v6']
yield (ipv4, ipv6, is_nat)
if __name__ == "__main__":
if (len(sys.argv) > 1):
f = open(sys.argv[1])
else:
f = sys.stdin
with f:
data = json.load(f)
result = parsejson(data)
for (ipv4, ipv6, is_nat) in result:
print("%s,%s" %(ipv4, is_nat))