etingof / pysnmp

Python SNMP library
http://snmplabs.com/pysnmp/
BSD 2-Clause "Simplified" License
576 stars 196 forks source link

Weird Mac Address format #400

Open arezazadeh opened 3 years ago

arezazadeh commented 3 years ago

Hi All,

I am using cmdBulk function to pull arp table, but it returns below

   "1.3.6.1.2.1.4.22.1.2.402.10.250.80.1": "\u00f8r\u00eak\u00c0?",
    "1.3.6.1.2.1.4.22.1.2.402.10.250.80.6": "\u0000 \u0085\u00eaFK",
    "1.3.6.1.2.1.4.22.1.2.402.10.250.80.7": "\u0000\u001d\u00ac\u0010\u0018c",
    "1.3.6.1.2.1.4.22.1.2.402.10.250.80.9": "\u0000#]v\u00d2A",

i am not sure what format is the MAC address. can anyone help me on this? my code is below

def get_bulk_oid(target, user_oid):
    print(f"Bulk Query the {target} for the oids...", "\n")
    oid = ObjectType(ObjectIdentity(user_oid))
    remote_host_dict = {}
    cdp_remote_host = bulkCmd(
        SnmpEngine(),
        UsmUserData('mycomm', authKey='mypass', privKey='mypass', authProtocol=hlapi.usmHMACMD5AuthProtocol,
                    privProtocol=hlapi.usmAesCfb128Protocol),
        UdpTransportTarget((target, 161)),
        ContextData(),
        0, 25,
        oid,
        lexicographicMode=False,
    )
    for errorIndication, errorStatus, errorIndex, varBinds in cdp_remote_host:
        if errorIndication:
            print(errorIndication)
            break
        elif errorStatus:
            print('%s at %s' % (errorStatus.prettyPrint(),
                                errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
            break
        else:
            for varBind in varBinds:
                remote_host_dict[str(varBind[0])] = str(varBind[1])
    return remote_host_dict
lextm commented 1 year ago

You shouldn't use str(varBind[1]) as the returned data are raw bytes for adapter physical address. prettyPrint might be used to print it out or you manually process the bytes. Converting to str without proper encoding only leads to useless output.

The actual definition of the physcal address is as below (from RFC1213-MIB or other valid source),

     PhysAddress ::=
       OCTET STRING
     -- This data type is used to model media addresses. For many
     -- types of media, this will be in a binary representation.
     -- For example, an ethernet address would be represented as
     -- a string of 6 octets.

     ipNetToMediaPhysAddress OBJECT-TYPE
       SYNTAX PhysAddress
       ACCESS read-write
       STATUS mandatory
       DESCRIPTION
           "The media-dependent `physical' address."
       ::= { ipNetToMediaEntry 2 }