Пример #1
0
def VLAN_map(filter_region=None):
    csv.register_dialect('csv', delimiter=';', quoting=csv.QUOTE_NONE)
    regions = {}
    with open(config.VLAN_PATH, 'r', encoding='utf-8-sig') as xl:
        result = csv.reader(xl, 'csv')

        for row in result:
            if row[0] != '' and row[1] == '':
                regions.update({transliterate(row[0]): []})
            elif len(row) == 16 and transliterate(row[15]) in regions:
                regions[transliterate(row[15])].append(row)
            else:
                regions.update({'description': row})

    if (os.path.isfile(config.VLAN_PATH_JSON)):
        os.remove(config.VLAN_PATH_JSON)

    json_file = open(config.VLAN_PATH_JSON, 'w', encoding='utf-8-sig')
    json_file.write(json.dumps(regions, indent=4, sort_keys=True))
    json_file.close()

    description = regions.pop('description')
    regions = VLAN_map_format(regions, description, filter_region)

    return regions
Пример #2
0
def Switches(region, vlans_map, xl_map):
    ip_list = None

    for street in vlans_map[transliterate(region[0])]:
        if street[8] == '/23':
            filter_ip = street[3].split('-')[-1].split('.')

            filter_ip = ".".join([filter_ip[0], filter_ip[1], filter_ip[2]])

        elif street[8] == '/22':
            filter_ip = street[3].split('-')[-1].split('.')

            filter_ip = ".".join(filter_ip[0:2:1])

        map_devices.map_filtration_init(filter_ip)
        # loaded items from map
        filtred_map = map_devices.map_load(config.MAP_LOCATION)

        # setup missing types
        new_types = device_type.add_device_types('Switch', filtred_map)

        if len(new_types) > 0:
            print("Switch added:", new_types)
            # get type list for ports
            ports.init_ports(new_types)

        # add new devices from map
        info_ip = device.device_name_SWITCH(filtred_map, xl_map, street)
        if len(info_ip) > 0:
            print("Switches()", info_ip)
            ip_list = ip_adresses.setup_ip(info_ip)

    return ip_list
Пример #3
0
def excel_map(fname, csv_file):
    if os.path.isfile(fname):
        os.remove(fname)
    map_xl = {}
    csv.register_dialect('csv', delimiter=';', quoting=csv.QUOTE_NONE)
    with open(csv_file, 'r', encoding='utf-8-sig') as xl:
        result = csv.reader(xl, 'csv')
        header = []
        for row in result:
            init_name = row[0]
            if init_name == 'P_STREET':
                header = row[7:len(row):1]
            else:
                hint = {}
                hint = hint_init({}.fromkeys([*header]), row[7:len(row):1])
                row[0] = transliterate(row[0])
                if len(row[4]) > 0:
                    row[3] = row[4]
                    if row[3] in map_xl:
                        print("excel_map row[3]:", row[3])
                    map_xl.update({row[3]: [row[0], row[1], init_name, hint]})
                elif len(row[3]) > 0:
                    row[4] = row[3]
                    if row[3] in map_xl:
                        print("excel_map row[3]:", row[3])
                    map_xl.update({row[3]: [row[0], row[1], init_name, hint]})
                if len(row[3]) == 0:
                    print({row[3]: [row[0], row[1]]})

    json_file = open(fname, 'w', encoding='utf-8-sig')
    json_file.write(json.dumps(map_xl, indent=4, sort_keys=True))
    json_file.close()

    return map_xl
Пример #4
0
def rename_removed():
    removed_dev = {}
    vlans_map, xl_map = loader_maps()

    regions = [
        'Орехово-Зуево',
        'Кабаново',
        'Куровское',
        'Демихово',
        'Ликино-Дулёво',
    ]
    for region in regions:
        for street in vlans_map[transliterate(region)]:
            if street[8] == '/23':
                filter_ip = street[3].split('-')[-1].split('.')

                filter_ip = ".".join(
                    [filter_ip[0], filter_ip[1], filter_ip[2]])

            elif street[8] == '/22':
                filter_ip = street[3].split('-')[-1].split('.')

                filter_ip = ".".join(filter_ip[0:2:1])

            map_devices.map_filtration_init(filter_ip)
            # loaded items from map
            filtred_map = map_devices.map_load(config.MAP_LOCATION)
            removed_dev.update({
                region:
                ReMoved.main(region, vlans_map, xl_map, filtred_map, net_box)
            })
    print()
    return removed_dev
Пример #5
0
def VLAN_map_format(regions, description, filter_region=None):

    for row in regions:
        if not row == 'description':
            for site in regions[row]:
                site[11] = '-'.join([site[9].split('-')[0], 'cctv', site[11]])
                site[1] = transliterate(site[1])
                site[13] = transliterate(site[13])
                site[11] = '-'.join([site[11], site.pop(12) + description[12]])
                site[9] = '-'.join([site[9], site.pop(10) + description[10]])
                site[7] = '-'.join([site[7], site.pop(8) + description[8]])
                site[5] = '-'.join([site[5], site.pop(6) + description[6]])
                site[3] = '-'.join([site[3], site.pop(4) + site[8]])
    if filter_region:
        regions = {transliterate(filter_region): regions[transliterate(filter_region)]}

    return regions
Пример #6
0
def pre_conf():
    ip_list = []

    region_list = [
        ('Кабаново', 'kb'),
        ('Демихово', 'dm'),
        ('Куровское', 'ku'),
        ('Ликино-Дулёво', 'ld'),
        ('Орехово-Зуево', 'oz'),
    ]

    for region, slug in region_list:
        regions.add_regions(transliterate(region), slug=slug)

    # vlans_map, xl_map = loader_maps('file', 'file')
    vlans_map, xl_map = loader_maps('load', 'load')

    for load in region_list:
        ip_list.append(Switches(load, vlans_map, xl_map))

    return list(zip((r for r, _ in region_list), ip_list))
Пример #7
0
def get_names():
    for init in map_scan:

        dev = map_scan[init]

        ip_address = dev.get('address')

        site_arr = xl_map.get(ip_address)

        if (dev.get('description') and site_arr):
            site_arr[3].update({'hint': dev.get('description')})
            desc_tmp = site_arr[3]
            dev['description'] = desc_tmp

        if not site_arr:
            continue

        number_house = re.sub('[,/]', '_',
                              dev.get('name').split()[-1].split('.')[0])
        site_name = (site_arr[0] + ' ' + number_house).strip()
        trans_name = site_arr[2]
        site_name = transliterate(site_name)
        site_info = net_box.dcim.sites.get(name=site_name.strip())

        if not site_info:
            site_info = net_box.dcim.sites.get(slug=slugify(site_name.strip()))

        region_info = net_box.dcim.regions.get(slug=region)

        names_regions = []
        region_tmp = region
        if not region_info:
            while region_tmp:

                name_this_region = net_box.dcim.regions.get(slug=region_tmp)
                names_regions.append(name_this_region.slug)
                region_tmp = name_this_region.parent
                if region_tmp:
                    region_tmp = region_tmp.slug
            names_regions = names_regions[-1]
        else:
            names_regions = region_info.slug

        name_prefix_tmp = dev.get('name').split('.')
        name_prefix_tmp.remove(name_prefix_tmp[0])
        name_prefix = '.'.join(name_prefix_tmp)

        name = '-'.join((names_regions, site_info.slug))
        if name_prefix:
            name = name + '.' + name_prefix

        removed = True

        if site_arr[3].get('P_REMOVED') == '1':
            dev_removed = net_box.dcim.devices.get(name="REMOVED " + name)
            if not dev_removed:
                removed = False
            elif not dev_removed.custom_fields('P_REMOVED'):
                removed = False
        else:
            dev_tmp = net_box.net_box.dcim.devices.get(name=name)
            if dev_tmp:
                if dev_tmp.custom_fields('P_REMOVED'):
                    removed = False

    if removed:
        renamed(name)

    return ''
Пример #8
0
def device_name_SWITCH(map_dev, xl_map, region):

    device_role = net_box.dcim.device_roles.get(name='Switch').id

    ip_mask = '/' + region[3].split('/')[-1]

    region = region[1].strip()

    result = []

    region = slugify(region)

    for init in map_dev:

        dev = map_dev[init]

        ip_address = dev.get('address')

        if net_box.ipam.ip_addresses.get(address=ip_address):
            continue

        site_arr = xl_map.get(ip_address)

        if (dev.get('description') and site_arr):
            site_arr[3].update({'hint': dev.get('description')})
            desc_tmp = site_arr[3]
            dev['description'] = desc_tmp

        if not site_arr:
            continue

        site_arr[3]['P_RESERVED3'] = str(site_arr[2]) + '_' + str(site_arr[1])

        number_house = transliterate(
            re.sub('[,/]', '_', site_arr[1].split()[-1].split('.')[0]))
        site_name = (site_arr[0] + ' ' + number_house).strip()
        trans_name = site_arr[2]
        try:
            prefix = re.match(r'^[\w-]+\.', site_name).group(0)
            if prefix in prefixes:
                site_name = re.sub(r'^[\w-]+\.', prefixes[prefix], site_name)
        except BaseException as ex:
            print(ex)

        site_info = net_box.dcim.sites.get(name=site_name.strip())

        if not site_info:
            site_info = net_box.dcim.sites.get(slug=slugify(site_name.strip()))
        region_info = net_box.dcim.regions.get(slug=region)

        if not region_info:
            raise ValueError(f"No region_info for {map_dev[init]}")

        if site_info:
            site = site_info
        else:
            site = sites.add_site(trans_name + ' ' + number_house, site_name,
                                  region)
        site_id = site.id

        names_regions = []
        region_tmp = region

        while region_tmp:

            name_this_region = net_box.dcim.regions.get(slug=region_tmp)
            names_regions.append(name_this_region.slug)
            region_tmp = name_this_region.parent
            if region_tmp:
                region_tmp = region_tmp.slug

        names_regions = names_regions[-1]

        name_prefix_tmp = site_arr[1].split('.')
        name_prefix_tmp.remove(name_prefix_tmp[0])
        name_prefix = transliterate('.'.join(name_prefix_tmp))

        name = '-'.join((names_regions, site.slug))
        if name_prefix:
            name = name + '.' + name_prefix

        if site_arr[3].get('P_REMOVED') == '1':
            removed = net_box.dcim.devices.get(name="REMOVED " + name)
            name = "REMOVED " + name
        else:
            removed = None

        name_type_tmp = dev.get('description')['hint'].split('\n')[0]
        name_type = re.sub(r'^\[font .*\]', '', name_type_tmp).split(' ')[0]
        type_dev = net_box.dcim.device_types.get(model='' + name_type)

        if type_dev:
            if not removed:
                type_id = type_dev.id
                description = dev.get('description')
                if description['P_REMOVED'] == '1':
                    description['P_REMOVED'] = True
                elif description['P_REMOVED'] == '0':
                    description['P_REMOVED'] = False
                if description['P_TRANSIT'] == '1':
                    description['P_TRANSIT'] = True
                elif description['P_TRANSIT'] == '0':
                    description['P_TRANSIT'] = False
                json_dev = {
                    "name": name,
                    "device_type": type_id,
                    "device_role": device_role,
                    "site": site_id,
                    "tags": config.TAGS,
                    "comments": description.pop('hint'),
                    "custom_fields": description
                }

                result.append([
                    json_dev, {
                        "primary_ip": ip_address + ip_mask,
                        "addresses": dev.get('addresses'),
                    }
                ])
            else:
                print('Dev has removed but in netbox')
        else:
            print('Не установлен Тип в config для данного устройства:',
                  name_type, name, ip_address)

    create_dev = add_devices(result)

    return create_dev