131 lines
4.3 KiB
Python
Executable File
131 lines
4.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
# This file is part of dnmgmt, a number resource management system
|
|
# Licensed under GNU General Public License v3 or later
|
|
# Written by Sebastian Lohff (seba@someserver.de)
|
|
|
|
import argparse
|
|
import json
|
|
|
|
import sys
|
|
import re
|
|
import os
|
|
import django
|
|
import ipaddress
|
|
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dnmgmt.settings")
|
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
sys.path.append(BASE_DIR)
|
|
django.setup()
|
|
|
|
|
|
from whoisdb.models import ASBlock, ASNumber, Contact, Maintainer, InetNum
|
|
|
|
__VERSION__ = '0.1'
|
|
|
|
def _parser():
|
|
parser = argparse.ArgumentParser(
|
|
#prog='foo',
|
|
#description='do some awesome foo',
|
|
)
|
|
|
|
#parser.add_argument("-p", "--port", default=2323, type=int, help="Your port")
|
|
#parser.add_argument("-v", "--verbose", default=False, action="store_true", help="Be more verbose")
|
|
parser.add_argument("-j", "--json", type=argparse.FileType('r'), required=True, help="Path to json file")
|
|
|
|
parser.add_argument("--version", action="version", version="%(prog)s " + __VERSION__)
|
|
|
|
return parser
|
|
|
|
def main():
|
|
parser = _parser()
|
|
args = parser.parse_args()
|
|
|
|
defContact = Contact.objects.get(handle="UNKNOWN-DN")
|
|
defMnt = Maintainer.objects.get(handle="DARKNET-MNT")
|
|
blocks = [
|
|
ASBlock.objects.get(handle="TRA1-ASB"),
|
|
ASBlock.objects.get(handle="UAB1-ASB")
|
|
]
|
|
ranges = [
|
|
InetNum.objects.get(handle="TRA1-NET"),
|
|
InetNum.objects.get(handle="DEF4-NET"),
|
|
InetNum.objects.get(handle="DEF6-NET"),
|
|
InetNum.objects.get(handle="MISC6-NET"),
|
|
InetNum.objects.get(handle="LTRA1-NET"),
|
|
InetNum.objects.get(handle="MAIN4-NET"),
|
|
]
|
|
|
|
|
|
data = json.load(args.json)
|
|
print(data.keys())
|
|
for k, v in data["handles"].items():
|
|
print(k, v["person"])
|
|
for asn in data["as"]:
|
|
print(asn)
|
|
if ASNumber.objects.filter(number=asn["number"]).count() > 0:
|
|
continue
|
|
|
|
obj = ASNumber(number=asn["number"])
|
|
block = None
|
|
for block in blocks:
|
|
if obj.number >= block.asBegin and obj.number <= block.asEnd:
|
|
obj.asblock = block
|
|
break
|
|
else:
|
|
raise ValueError("AS %d does not fit a block" % asn["number"])
|
|
|
|
if len(asn['admin_c']) >= 1:
|
|
name = data["handles"][asn["admin_c"][0]]["person"]
|
|
obj.name = "Imported AS of %s" % name
|
|
obj.handle = ASNumber.genGenericHandle(re.sub("[^a-zA-Z0-9 ]", "", name))
|
|
else:
|
|
obj.name = "Imported AS without admin info"
|
|
obj.handle = ASNumber.genGenericHandle("Unknown Imported AS")
|
|
obj.description = "Object has been imported from old DB and has not yet been edited"
|
|
obj.save()
|
|
obj.mnt_by.add(defMnt)
|
|
obj.admin_c.add(defContact)
|
|
obj.save()
|
|
|
|
for net in data["network"]:
|
|
print(net)
|
|
network = ipaddress.ip_network(net["prefix"])
|
|
|
|
if InetNum.objects.filter(address=str(network.network_address), netmask=network.prefixlen).count() > 0:
|
|
continue
|
|
|
|
origin = None
|
|
if net["origin"]:
|
|
origin = ASNumber.objects.get(number=net["origin"])
|
|
obj = InetNum(address=str(network.network_address), netmask=network.prefixlen)
|
|
obj.protocol = InetNum.IPv4 if network.version == 4 else InetNum.IPv6
|
|
|
|
x = list(filter(lambda _x: _x['number'] == net["origin"], data["as"]))
|
|
if len(x) > 0 and x[0]["admin_c"]:
|
|
name = data["handles"][x[0]["admin_c"][0]]["person"]
|
|
obj.name = "Imported Network of %s" % name
|
|
obj.handle = InetNum.genGenericHandle(re.sub("[^a-zA-Z0-9 ]", "", name))
|
|
else:
|
|
obj.name = "Imported Network without admin info"
|
|
obj.handle = InetNum.genGenericHandle("Unknown Imported Network")
|
|
obj.description = "Object has been imported from old DB and has not yet been edited"
|
|
|
|
for r in ranges:
|
|
if network.network_address in r.getNetwork():
|
|
obj.parent_range = r
|
|
break
|
|
else:
|
|
raise ValueError("%s did not fit in any netblock" % network)
|
|
|
|
obj.save()
|
|
|
|
obj.mnt_by.add(defMnt)
|
|
obj.admin_c.add(defContact)
|
|
if origin:
|
|
obj.origin_as.add(origin)
|
|
obj.save()
|
|
|
|
if __name__ == '__main__':
|
|
main()
|