'PyMongo and Unifi Controller VLAN

I am a beginner in python and coding in general. I am learning since 1 month.

Maybe you guys can help me out. I wanted to learn python for a long time now and the problem was i never really had an idea on where i could actually use it. So now i had this idea of a making an Documentation Tool for Switch VLANs. (Unifi Only)

So far my code looks like this:

from pymongo import MongoClient
import json

client = MongoClient('mongodb://localhost:27117/')
dbname = ('ace')
db = client[dbname]
#Cursor and Collections for Devices and Ports
devicecoll = db['device']
devicecursor = devicecoll.find({'type': 'usw'})
portcoll = db['portconf']
portcursor = portcoll.find()
#Empty Lists to fill
switchlist = []
portlist = []

class Switch:
    def __init__(self, ip, model, name, override):
        self.override = override
        self.name = name
        self.model = model
        self.ip = ip

    def __str__(self):
        return f'{self.ip}, {self.model}, {self.name} {self.override}'


class Ports:
    def __init__(self, port_id, name):
        self.name = name
        self.port_id = port_id

    def __str__(self):
        return f'{self.port_id}, {self.name}'

#Device Loops
for device in devicecursor:
    newswitch = (Switch(ip=device['ip'], model=device['model'], name=device['name'], override=device['port_overrides']))
    switchlist.append(newswitch)


#Port Loops
for port in portcursor:
    newport = (Ports(port_id=port['_id'], name=port['name']))
    portlist.append(newport)


print(switchlist[0])
print(portlist[0])

This gives me an output like this:

10.68.2.11, USXG, Switch01 Keller [{'port_idx': 9, 'portconf_id': '**5ddfb6f551bc6a02b3b3f375**'}, {'port_idx': 10, 'portconf_id': '**5ddfb6f551bc6a02b3b3f375**'}, {'port_idx': 11, 'portconf_id': '5f195db3e2d1ae029bd7366a'}, {'port_idx': 12, 'portconf_id': '5f195db3e2d1ae029bd7366a'}, {'port_idx': 14, 'portconf_id': '5f195db3e2d1ae029bd7366a'}, {'port_idx': 15, 'portconf_id': '5f195db3e2d1ae029bd7366a'}, {'port_idx': 16, 'portconf_id': '5f195db3e2d1ae029bd7366a'}]


**5ddfb6f551bc6a02b3b3f375**, All

Is there an easy way to loop through every port on the switches and use the port_idx and connect them with the id in the portlist.

I want to put this into a csv or xls file later in a format like this: Switch Port: VLAN:

Thanks in advance. Kind regards.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source