[pysnmp-users] decoding MAC values in snmptable.py
Brought to you by:
elie
From: John D. <joh...@mn...> - 2002-09-18 17:47:32
|
A list for users of pure-Python SNMP framework <pysnmp-users.lists.sourceforge.net> List-Unsubscribe: <https://lists.sourceforge.net/lists/listinfo/pysnmp-users>, <mailto:pys...@li...?subject=unsubscribe> List-Archive: <http://sourceforge.net/mailarchives/forum.php?forum=pysnmp-users> Here's how I did it (from snmptable.py): # import binascii # ... later, in the same code ... (answer, src) = client.send_and_receive(\ req.encode(community=args[1], encoded_oids=encoded_oids)) # Attempt to decode SNMP response rsp.decode(answer) # Make sure response matches request (request IDs, communities, etc) if req != rsp: raise 'Unmatched response: %s vs %s' % (str(req), str(rsp)) # Decode BER encoded Object IDs. oids = map(lambda x: x[0], map(asn1.OBJECTID().decode, \ rsp['encoded_oids'])) # Decode BER encoded values associated with Object IDs. vals = map(lambda x: x[0](), map(asn1.decode, rsp['encoded_vals'])) # ... various error checking and cruft skimming skipped over here ... # Print out results for (oid, val) in map(None, oids, vals): print oid + ' ----> ', binascii.b2a_hex(val) .1.3.6.1.2.1.17.4.3.1.1.0.2.185.184.176.0 ----> 0002b9b8b000 snmptable.py gets the 'vals' list like this: vals = map(lambda x: x[0](), map(asn1.decode, rsp['encoded_vals'])) The vals list ends up looking like this, for example: vals: ['\x00\x02\xb9\xb8\xb0\x00'] Each element in that list (in this case a list with one element), is a non-packed sequence of 'raw' bytes. Notice that the above looks like an ethernet address, but when you print it, it comes out as various non-printables. By using bin2ascii.b2a_hex, you can print the hexadecimal representation of the byte sequence. Info on using snmp to get ethernet address table info can be found at: http://www.cisco.com/warp/public/477/SNMP/cam_snmp.shtml From the above url, the bad news is that you must query the 'CAM' or mac address table for each lan on a cisco switch via snmp to get an accurate mac address table, similar to what the ios commands 'sho cam dynamic', or 'sho mac-address-table dynamic'would give you. here is the oid to get vlan assignments on a cisco switch: .1.3.6.1.4.1.9.9.68.1.2.2.1.2 To get that table, for example, $snmpwalk my-switch.domain.name public .1.3.6.1.4.1.9.9.68.1.2.2.1.2 enterprises.9.9.68.1.2.2.1.2.1 = 9 enterprises.9.9.68.1.2.2.1.2.2 = 9 enterprises.9.9.68.1.2.2.1.2.3 = 9 enterprises.9.9.68.1.2.2.1.2.4 = 9 enterprises.9.9.68.1.2.2.1.2.5 = 9 enterprises.9.9.68.1.2.2.1.2.6 = 9 enterprises.9.9.68.1.2.2.1.2.7 = 9 enterprises.9.9.68.1.2.2.1.2.8 = 9 enterprises.9.9.68.1.2.2.1.2.9 = 9 enterprises.9.9.68.1.2.2.1.2.10 = 9 enterprises.9.9.68.1.2.2.1.2.11 = 9 enterprises.9.9.68.1.2.2.1.2.12 = 9 enterprises.9.9.68.1.2.2.1.2.13 = 9 enterprises.9.9.68.1.2.2.1.2.14 = 9 enterprises.9.9.68.1.2.2.1.2.15 = 9 enterprises.9.9.68.1.2.2.1.2.16 = 9 enterprises.9.9.68.1.2.2.1.2.17 = 9 enterprises.9.9.68.1.2.2.1.2.18 = 9 enterprises.9.9.68.1.2.2.1.2.19 = 9 enterprises.9.9.68.1.2.2.1.2.20 = 9 enterprises.9.9.68.1.2.2.1.2.21 = 9 enterprises.9.9.68.1.2.2.1.2.22 = 9 enterprises.9.9.68.1.2.2.1.2.23 = 9 enterprises.9.9.68.1.2.2.1.2.24 = 9 enterprises.9.9.68.1.2.2.1.2.25 = 9 enterprises.9.9.68.1.2.2.1.2.26 = 9 enterprises.9.9.68.1.2.2.1.2.27 = 9 enterprises.9.9.68.1.2.2.1.2.28 = 9 enterprises.9.9.68.1.2.2.1.2.29 = 9 enterprises.9.9.68.1.2.2.1.2.30 = 9 enterprises.9.9.68.1.2.2.1.2.31 = 9 enterprises.9.9.68.1.2.2.1.2.32 = 9 enterprises.9.9.68.1.2.2.1.2.33 = 9 enterprises.9.9.68.1.2.2.1.2.34 = 9 enterprises.9.9.68.1.2.2.1.2.35 = 9 enterprises.9.9.68.1.2.2.1.2.36 = 9 enterprises.9.9.68.1.2.2.1.2.37 = 9 enterprises.9.9.68.1.2.2.1.2.38 = 9 enterprises.9.9.68.1.2.2.1.2.39 = 9 enterprises.9.9.68.1.2.2.1.2.40 = 9 enterprises.9.9.68.1.2.2.1.2.41 = 9 enterprises.9.9.68.1.2.2.1.2.42 = 9 enterprises.9.9.68.1.2.2.1.2.43 = 39 enterprises.9.9.68.1.2.2.1.2.44 = 9 enterprises.9.9.68.1.2.2.1.2.45 = 40 enterprises.9.9.68.1.2.2.1.2.46 = 9 enterprises.9.9.68.1.2.2.1.2.47 = 9 enterprises.9.9.68.1.2.2.1.2.48 = 9 enterprises.9.9.68.1.2.2.1.2.50 = 2 snmptable.py also get the above info must fine, except the 'encoded_vals' part of the rsp object isn't a raw sequence of bytes representing an ethernet address - its just the vlan number. The above was executed against a catalyst 2950G 48 port switch. I'm putting this info in this message, because finding the oid was a lot of work, so this should save somebody out there a lot of time. Hope this helps, joh...@mn... |