From 685d8ccca54bb325bc4c16b8cf2d6390dafd288a Mon Sep 17 00:00:00 2001 From: acidvegas Date: Sat, 21 Oct 2023 16:17:50 -0400 Subject: [PATCH] Added RIPE Live bgp --- examples/bgp.py | 26 ++++++++++++++++++++++++++ examples/ris-live.js | 25 +++++++++++++++++++++++++ ris-live.py | 26 ++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 examples/bgp.py create mode 100644 examples/ris-live.js create mode 100644 ris-live.py diff --git a/examples/bgp.py b/examples/bgp.py new file mode 100644 index 0000000..e6859ef --- /dev/null +++ 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 new file mode 100644 index 0000000..26cb83a --- /dev/null +++ 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 new file mode 100644 index 0000000..264e8b8 --- /dev/null +++ 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'])