pysnmp

Need to get mac address tables from d-link switches with python


Probably I should use SNMP v2c and pysnmp, but can't figure out how can I do that clearly, probably I need a list of port to mac array dictionaries. Anyways I am going to push that into mongodb and then get the port number having the mac address.

Here is the shell command that gives that information

snmpwalk -O0sUX -v2c -Cc -c public 10.77.10.8 BRIDGE-MIB::dot1dTpFdbPort

update: https://github.com/edikmkoyan/portmatrix/blob/master/portmatrix.py


Solution

  • I'd suggest starting from a script like this. It's not debugged, so if you get it fixed to a fully working condition - please comment and I'd update the code to benefit others.

    from pysnmp.hlapi import *
    
    for (errorIndication,
         errorStatus,
         errorIndex,
         varBindTable) in bulkCmd(SnmpEngine(),
                                  CommunityData('public'),
                                  UdpTransportTarget(('demo.pysnmp.com', 161)),
                                  ContextData(),
                                  0, 25,
                                  ObjectType(ObjectIdentity('BRIDGE-MIB', 'dot1dTpFdbPort')),
                                  lexicographicMode=False):
        if errorIndication:
            Exception(errorIndication)
        elif errorStatus:
            Exception(errorStatus)
        else:
            for varBinds in varBindTable:
                for varBind in varBinds:
                    # imaginary MongoDB document mapping port-ID -> MAC
                    db.portsAndMacs.insert_one(
                        {varBind[1].prettyPrint(): varBind[0].prettyPrint()[-17:]}
                    )