dnmgmt/bin/import-data

112 lines
3.2 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
import json
import sys
import re
import os
import django
import ipaddress
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dnmgmt.settings")
sys.path.append("..")
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="DURC1-DN")
defContact
defMnt = Maintainer.objects.get(handle="DARK1-MNT")
blocks = [
ASBlock.objects.get(handle="TRA1-ASB"),
ASBlock.objects.get(handle="UAB1-ASB")
]
ASNumber
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 from %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.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 from %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"
obj.save()
obj.mnt_by.add(defMnt)
if origin:
obj.origin_as.add(origin)
obj.save()
if __name__ == '__main__':
main()