bgp

- research & experiments with border gateway protocol
git clone git://git.acid.vegas/bgp.git
Log | Files | Refs | Archive | README

commit 685d8ccca54bb325bc4c16b8cf2d6390dafd288a
parent fd144336035aa620187e8e69f03b0a589e5e3210
Author: acidvegas <acid.vegas@acid.vegas>
Date: Sat, 21 Oct 2023 16:17:50 -0400

Added RIPE Live bgp

Diffstat:
Aexamples/bgp.py | 26++++++++++++++++++++++++++
Aexamples/ris-live.js | 25+++++++++++++++++++++++++
Aris-live.py | 26++++++++++++++++++++++++++

3 files changed, 77 insertions(+), 0 deletions(-)

diff --git a/examples/bgp.py b/examples/bgp.py
@@ -0,0 +1,26 @@
+import pybgpstream
+from collections import defaultdict
+
+def fetch_asn_ip_mappings():
+    asn_ip_mappings = defaultdict(list)
+    stream = pybgpstream.BGPStream(
+        project="ris-live",
+        filter="collector rrc00",
+    )
+    for rec in stream:
+        for elem in rec:
+            if elem.type == "A":
+                origin_asn = elem.fields["as-path"].split()[-1]
+                prefix = elem.fields["prefix"]
+                if prefix not in asn_ip_mappings[origin_asn]:
+                    asn_ip_mappings[origin_asn].append(prefix)
+                    print(f'{origin_asn} - {prefix}')
+    return asn_ip_mappings
+
+if __name__ == "__main__":
+    mappings = fetch_asn_ip_mappings()
+    for asn, prefixes in mappings.items():
+        print(f"ASN: {asn}, Prefixes: {', '.join(prefixes)}")
+
+
+
diff --git a/examples/ris-live.js b/examples/ris-live.js
@@ -0,0 +1,25 @@
+/*
+Subscribe to a RIS Live stream and output every message to the javascript console.
+
+The exact same code will work in Node.js after running 'npm install ws' and including the following line:
+
+const WebSocket = require('ws');
+*/
+var ws = new WebSocket("wss://ris-live.ripe.net/v1/ws/?client=js-example-1");
+var params = {
+    moreSpecific: true,
+    host: "rrc21",
+    socketOptions: {
+        includeRaw: true
+    } 
+};
+ws.onmessage = function(event) {
+    var message = JSON.parse(event.data);
+    console.log(message.type, message.data);
+};
+ws.onopen = function() {
+    ws.send(JSON.stringify({
+        type: "ris_subscribe",
+        data: params
+    }));
+};
diff --git a/ris-live.py b/ris-live.py
@@ -0,0 +1,26 @@
+import json
+
+try:
+	import websocket
+except ImportError:
+	raise SystemExit('missing websocket-client')
+
+ws = websocket.WebSocket()
+ws.connect('wss://ris-live.ripe.net/v1/ws/?client=bgpexample')
+
+params = {
+	'moreSpecific': True,
+	'host': 'rrc21',
+	'socketOptions': {
+		'includeRaw': True
+	}
+}
+
+ws.send(json.dumps({
+		'type': 'ris_subscribe',
+		'data': params
+}))
+
+for data in ws:
+	parsed = json.loads(data)
+	print(parsed['type'], parsed['data'])