-
Notifications
You must be signed in to change notification settings - Fork 3
/
ripe-stream.py
executable file
·159 lines (148 loc) · 5.76 KB
/
ripe-stream.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
""" Subscribe to the RIPE RIS-Live stream and process it """
## (c) 2019 Jared Mauch
import json
import time
import radix
import sys
import websocket
## Uncomment and update this with a unique identifier for the RIPE team
#url = "wss://ris-live.ripe.net/v1/ws/?client=username_at_example_com"
tree = radix.Radix()
## File format : CIDR,METADATA,METADATA2
prefix_file = "/path/to/file/prefixes.csv"
# load the prefixes to memory
with open(prefix_file) as fp:
line = fp.readline()
while line:
## parse line
parse_me = str(line.rstrip())
(prefix, metadata, metadata2) = parse_me.split(',', 3)
rnode = tree.add(prefix)
rnode.data['metadata'] = metadata
rnode.data['metadata2'] = metadata2
## fetch another line
line = fp.readline()
while True:
ws = websocket.WebSocket()
ws.connect(url)
# subscribe to all BGP Updates
ws.send(json.dumps({"type": "ris_subscribe", "data": {"type": "UPDATE"}}))
try:
for data in ws:
parsed = json.loads(data)
if parsed.get('type', None) == 'ris_error':
print(data)
if parsed.get('type', None) == 'ris_message':
# print(parsed["type"], parsed["data"])
parsed_data = parsed.get("data", None)
announcements = parsed_data.get('announcements', None)
withdrawls = parsed_data.get('withdrawls', None)
try:
as_path = ' '.join(str(x) for x in parsed_data.get('path', None))
except:
as_path = ''
if announcements is not None:
for announcement in announcements:
for prefix in announcement['prefixes']:
try:
rnode = tree.search_best(prefix)
except Exception as e:
print("search_best: %s returned " % prefix, e)
if rnode is not None and rnode.data['metadata'] is not None:
# Is it a more specific?
if prefix != rnode.prefix:
sub_prefix = "Yes"
else:
sub_prefix = ""
print("add|%s|%s|%s|%s|%s|%s" % (prefix, sub_prefix, rnode.prefix,
rnode.data['metadata'], rnode.data['metadata2'], as_path))
if withdrawls is not None:
for announcement in withdrawls:
for prefix in announcement['prefixes']:
try:
rnode = tree.search_best(prefix)
except Exception as e:
print("search_best: %s returned " % prefix, e)
if rnode is not None and rnode.data['metadata'] is not None:
# Is it a more specific?
if prefix != rnode.prefix:
sub_prefix = "Yes"
else:
sub_prefix = ""
print("del|%s|%s|%s|%s|%s|%s" % (prefix, sub_prefix, rnode.prefix,
rnode.data['metadata'], rnode.data['metadata2'], as_path))
except websocket.WebSocketConnectionClosedException as e:
print("Disconnected, sleeping for a few then reconnect", e)
time.sleep(30)
except ConnectionResetError as e:
print("Disconnected, sleeping for a few then reconnect", e)
time.sleep(30)
except BrokenPipeError as e:
print("Disconnected, sleeping for a few then reconnect", e)
time.sleep(30)
except websocket.WebSocketBadStatusException as e:
print("Disconnected, sleeping for a few then reconnect", e)
time.sleep(30)
except websocket.WebSocketTimeoutException as e:
print("Disconnected, sleeping for a few then reconnect", e)
time.sleep(30)
except KeyboardInterrupt:
print("User stop requested")
sys.exit()
except Exception as e:
print("some other error?", e)
time.sleep(30)
##
## ris_message
### {'timestamp': 1550258410.78,
### 'peer': '217.29.66.88',
### 'peer_asn': '20811',
### 'id': '217.29.66.88-1550258410.78-59479614',
### 'host': 'rrc10',
### 'type': 'UPDATE',
### 'path': [20811, 8529, 9155, 51914, 51914, 51914, 51914],
### 'origin': 'igp',
### 'announcements':
### [{'next_hop': '217.29.67.63', 'prefixes': ['91.221.128.0/24']} ]
### }
## ris_message
### {'timestamp': 1550258410.78,
### 'peer': '217.29.66.88',
### 'peer_asn': '20811',
### 'id': '217.29.66.88-1550258410.78-59479616',
### 'host': 'rrc10',
### 'type': 'UPDATE',
### 'path': [20811, 8529, 49666, 42440, 205647, 44400, 47843],
### 'origin': 'igp',
### 'announcements': [
#### {'next_hop': '217.29.67.63',
#### 'prefixes':
##### ['87.248.144.0/24',
##### '87.248.150.0/24',
##### '87.248.139.0/24',
##### '87.248.153.0/24',
##### '87.248.149.0/24',
##### '87.248.145.0/24',
##### '87.248.152.0/24',
##### '87.248.151.0/24',
##### '87.248.138.0/24',
##### '87.248.133.0/24',
##### '87.248.147.0/24',
##### '87.248.155.0/24',
##### '87.248.131.0/24',
##### '87.248.132.0/24',
##### '87.248.136.0/24',
##### '87.248.154.0/24',
##### '87.248.156.0/24',
##### '87.248.158.0/24',
##### '87.248.134.0/24',
##### '87.248.135.0/24',
##### '87.248.129.0/24',
##### '87.248.130.0/24',
##### '87.248.146.0/24',
##### '87.248.128.0/24',
##### '87.248.137.0/24']
#### }
### ]
### }