Add ripe.sh for creating routes, update ripe.py

This commit is contained in:
Nico Schottelius 2022-03-18 10:09:50 +01:00
parent d7fc74a3f8
commit 1e3f47503a
2 changed files with 132 additions and 10 deletions

63
ripe.py
View file

@ -1,8 +1,19 @@
#!/usr/bin/env python3 #!/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 json
import os
import urllib.request
import copy
import ipaddress
import requests
import sys
base_object = { base_object = {
"objects": { "objects": {
@ -30,20 +41,52 @@ base_object = {
def gen_route6(route6, asn): def gen_python_obj(attributes):
obj = base_object.copy() obj = copy.deepcopy(base_object)
asn_obj = { 'name': 'origin', 'value': asn } for name in attributes.keys():
route6_obj = { 'name': 'origin', 'value': asn } attribute = {
'name': name,
'value': attributes[name]
}
obj['objects']['object'][0]['attributes']['attribute'].append(asn_obj) obj['objects']['object'][0]['attributes']['attribute'].append(attribute)
obj['objects']['object'][0]['attributes']['attribute'].append(route6_obj)
return obj 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): def to_json(obj):
return json.dumps(obj, indent = 4) return json.dumps(obj, indent = 4)
if __name__ == '__main__': if __name__ == '__main__':
for route6 in [ "2a0a:e5c0::/48" ]: password = os.environ['RIPE_API_PASSWORD']
print(to_json(gen_route6(route6, "AS213081")))
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)))

79
ripe.sh Executable file
View file

@ -0,0 +1,79 @@
#!/bin/sh
# 2022-03-18, ungleich (foss at ungleich.ch)
# Copying: GPL3+
if [ $# -lt 2 ] ; then
echo "$0 network asn [nodryrun]"
exit 1
fi
set -x
network=$1; shift
asn=$1; shift
if [ $# -ge 1 ]; then
dryrun=""
else
dryrun="&dry-run"
fi
form=$(mktemp)
case ${network} in
*:*)
obj_type=route6
;;
*.*)
obj_type=route
;;
*)
echo "No idea what to do with $network"
exit 1
;;
esac
cat > $form <<EOF
{
"objects": {
"object": [
{
"source": {
"id": "RIPE"
},
"attributes": {
"attribute": [
{
"name": "${obj_type}",
"value": "${network}"
},
{
"name": "mnt-by",
"value": "mnt-ungleich"
},
{
"name": "source",
"value": "RIPE"
},
{
"name": "origin",
"value": "${asn}"
}
]
}
}
]
}
}
EOF
cat ${form}
curl -X POST \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
--data @${form} \
"https://rest.db.ripe.net/ripe/${obj_type}?password=${RIPE_API_PASSWORD}${dryrun}"
rm -f ${form}