diff --git a/ripe.py b/ripe.py index afcbfd7..48f5561 100644 --- a/ripe.py +++ b/ripe.py @@ -1,8 +1,19 @@ #!/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": { @@ -30,20 +41,52 @@ base_object = { -def gen_route6(route6, asn): - obj = base_object.copy() +def gen_python_obj(attributes): + obj = copy.deepcopy(base_object) - asn_obj = { 'name': 'origin', 'value': asn } - route6_obj = { 'name': 'origin', 'value': asn } + for name in attributes.keys(): + attribute = { + 'name': name, + 'value': attributes[name] + } - obj['objects']['object'][0]['attributes']['attribute'].append(asn_obj) - obj['objects']['object'][0]['attributes']['attribute'].append(route6_obj) + 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__': - for route6 in [ "2a0a:e5c0::/48" ]: - print(to_json(gen_route6(route6, "AS213081"))) + 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))) diff --git a/ripe.sh b/ripe.sh new file mode 100755 index 0000000..76d5620 --- /dev/null +++ b/ripe.sh @@ -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 <