import json import logging import os.path import pynetbox from oxi.manager import OxidizedAPI from settings import settings from type import vimpelcomIPaddress374, vimpelcomIPaddressSSPD, vimpelcomIPaddressTEST logging.basicConfig() log = logging.getLogger() netbox = pynetbox.api( settings.nb_url, token=settings.nb_token) netbox.http_session.verify = False filters = { "has_primary_ip": "true", "tenant": "vimpelcom", "role": "Kommutator", } oxi = OxidizedAPI(username=settings.oxi_username, password=settings.oxi_password, verify=False) devices = netbox.dcim.devices.filter(**filters) device_list = [item.name for item in devices] devices = netbox.dcim.devices.filter(**filters) if not os.path.exists('devices.json'): with open('devices.json', 'w') as file: json.dump(device_list, file, ensure_ascii=False, indent=3) for device in devices: if device.name not in device_list: continue ex_interface = netbox.dcim.interfaces.filter(device=device.name) log.debug("device: %r", device.name) try: oxidized_device = oxi.node(device.name.replace("(1)", "")) except ValueError: log.warning("%r no exist in Oxidized", device.name) continue log.debug("Vlan-interfaces: %r", oxidized_device.config.vlaninterfaces()) if oxidized_device.config.vlaninterfaces(): for vlan_interface in oxidized_device.config.vlaninterfaces(): log.debug("IPaddress: %r", vlan_interface.ip_address) checker = netbox.dcim.interfaces.get( name=vlan_interface.interface, device=device.name ) if checker: continue child_interface = netbox.dcim.interfaces.create( name=vlan_interface.interface, type='virtual', device=device.id ) print(device) print("description: ", vlan_interface.description) if vlan_interface.description: child_interface.description = vlan_interface.description child_interface.save() print("child_interface: ", child_interface) print("ip address:", vlan_interface.ip_address) if vlan_interface.ip_address: netbox_ip = netbox.ipam.ip_addresses.create( address=str(vlan_interface.ip_address), tenant=device.tenant.id, ) if vlan_interface.ip_address in vimpelcomIPaddress374.prefix: netbox_ip.vrf = netbox.ipam.vrfs.get(name=vimpelcomIPaddress374.vrf).id elif vlan_interface.ip_address in vimpelcomIPaddressSSPD.prefix: netbox_ip.vrf = netbox.ipam.vrfs.get(name=vimpelcomIPaddressSSPD.vrf).id else: netbox_ip.vrf = netbox.ipam.vrfs.get(name=vimpelcomIPaddressTEST.vrf).id netbox_ip.assigned_object = child_interface netbox_ip.assigned_object_id = child_interface.id netbox_ip.assigned_object_type = 'dcim.interface' netbox_ip.save() device_list.remove(device.name) with open('devices.json', 'w') as file: json.dump(device_list, file, ensure_ascii=False, indent=3) # child_interface.save()