diff --git a/ungleich_ripe.py b/ungleich_ripe.py index 0648b58..51fe966 100644 --- a/ungleich_ripe.py +++ b/ungleich_ripe.py @@ -1,16 +1,89 @@ import argparse +import ipaddress +import json +import urllib.request +import pprint + +# RIPE_URL = "https://rest.db.ripe.net/{source}/{objecttype}/{key}" +RIPE_URL = "https://rest.db.ripe.net/ripe" +RIPE_URL = "https://rest-test.db.ripe.net/test" class ungleichRIPE(object): def __init__(self, parser, parents): self.parser = parser - self.parser['ripe-route6'] = self.parser['sub'].add_parser( - 'ripe-route6', + self.parser['ripe-add-route6'] = self.parser['sub'].add_parser( + 'ripe-add-route6', parents=[parents], help="Create route6 object in the ripe database") - self.parser['ripe-route6'].add_argument('--network', required=True) - self.parser['ripe-route6'].set_defaults(func=self.route6) + self.parser['ripe-add-route6'].add_argument('--network', + required=True) + self.parser['ripe-add-route6'].add_argument('--description', + required=True) + self.parser['ripe-add-route6'].add_argument('--password', + required=True, + help="Password for accessing the RIPE rest API") + self.parser['ripe-add-route6'].set_defaults(func=self.route6_add) - def route6(self, args): - print("Adding a v6 route object at RIPE for {}".format(args.network)) + def route6_add(self, args): + try: + net = ipaddress.IPv6Network(args.network) + except Exception as e: + print("Sorry, {} does not look like an IPv6 network: {}".format(args.network, e)) + raise + + url = "{}/route6/?password={}".format(RIPE_URL, args.password) + + ripe_object = {} + ripe_object['route6'] = args.network + ripe_object['origin'] = "AS209898" + ripe_object['descr'] = args.description + ripe_object['mnt-by'] = "mnt-ungleich" + +# ripe_attributes = [] + ripe_attributes = [{ "name": key, "value": value } for key, value in ripe_object.items() ] + + # for key, value in ripe_object.items(): + # ripe_attributes.append( { "name": key, + # "value": value + # } + # ) + + # "source": { + # "id": "RIPE" + # }, + + ripe_element = {} + ripe_element['objects'] = [] + ripe_element['objects'].append( + { "object": + [ + { + "attributes": { + "attribute": ripe_attributes + } + } + ] + } + ) + + data = json.dumps(ripe_element).encode('utf-8') + pprint.pprint(ripe_element) + + method = 'POST' + + req = urllib.request.Request(url=url, + data=data, + method='POST', + headers={ + "Content-Type": "application/json", + "Accept": "application/json" + }) + + pprint.pprint(req) + + print("Adding a v6 route object at {} for {} with {} req={}".format(url, args.network, data, str(req))) + + with urllib.request.urlopen(req) as f: + print(f.read().decode('utf-8')) diff --git a/ungleichcli.py b/ungleichcli.py index 792c35a..85c61f9 100644 --- a/ungleichcli.py +++ b/ungleichcli.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + import argparse from ungleich_dns import ungleichDNS