ripe/ripe.py
2022-03-18 10:09:50 +01:00

92 lines
2.2 KiB
Python

#!/usr/bin/env python3
# ungleich (foss at ungleich.ch), 2022-03-17
# Copying: GPLv3+
# RIPE URLs:
# https://rest.db.ripe.net/{source}/{objecttype}/{key}
# https://rest-test.db.ripe.net/test
# curl -X POST -H 'Content-Type: application/json' -H 'Accept: application/json' --data @route.txt 'https://rest.db.ripe.net/ripe/route6?password=...'
# https://rest.db.ripe.net/ripe/route6?dry-run&password=...
import json
import os
import urllib.request
import copy
import ipaddress
import requests
import sys
base_object = {
"objects": {
"object": [
{
"source": {
"id": "RIPE"
},
"attributes": {
"attribute": [
{
"name": "mnt-by",
"value": "mnt-ungleich"
},
{
"name": "source",
"value": "RIPE"
}
]
}
}
]
}
}
def gen_python_obj(attributes):
obj = copy.deepcopy(base_object)
for name in attributes.keys():
attribute = {
'name': name,
'value': attributes[name]
}
obj['objects']['object'][0]['attributes']['attribute'].append(attribute)
return obj
def gen_obj(objecttype, attributes, password):
ripe_url = f'https://rest.db.ripe.net/ripe/{objecttype}?dry-run&password={password}'
data = gen_python_obj(attributes)
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
print(to_json(data))
print(f"r/d/h: {ripe_url} - {data} - {headers}")
sys.exit(1)
r = requests.post(ripe_url, data, headers=headers)
print(r)
def to_json(obj):
return json.dumps(obj, indent = 4)
if __name__ == '__main__':
password = os.environ['RIPE_API_PASSWORD']
for route6 in [ "2a0a:e5c1::/32" ]:
attr = { "origin": "AS213081",
"route6": route6 }
print(gen_obj("route6", attr, password))
sys.exit(0)
for net in [ "185.203.112.0/22",
"147.78.192.0/22" ]:
v4net = ipaddress.IPv4Network(net)
for subnet in v4net.subnets(new_prefix=24):
attr = { "origin": "AS213081",
"route": str(subnet) }
print(to_json(gen_obj("route", subnet, attr)))