For When You Can't Have The Real Thing
[ start | index | login ]
start > dave > experiments > Switch Topology > python pysnmp

python pysnmp

Created by dave. Last edited by dave, 11 years and 125 days ago. Viewed 2,533 times. #7
[diff] [history] [edit] [rdf]
labels
attachments

Why?

Well I wanted a database, and the only interface I had through perl was their pseudo-OO interface. I figured if I have to mess around with objects, why restrict myself to half-assed perl objects when I could use whole-assed python objects instead?

Notes

  • Required: pyasn1 (0.0.6a used here) and pycrypto (2.0.1 used here)
  • Perl likes periods in its OID strings; python likes commas:
tpFdbAddressOID = (1,3,6,1,2,1,17,4,3,1,1)
tpFdbPortOID    = (1,3,6,1,2,1,17,4,3,1,2)
ethPortNameOID  = (1,3,6,1,2,1,2,2,1,2)
ethArpOID       = (1,3,6,1,2,1,4,22,1,2)
  • Working code lifted pretty much verbatum from >>http://pysnmp.sourceforge.net/docs/4.1.x/index.html#ONELINER-APPS -- pretty slickly done.
  • Performance: Scanning the same snmp tables through pysnmp as was done with Perl through Net::SNMP takes longer: perl took 1m 15s, python took 2m 5s. So performance FTL. This might be because python connects every time for each table scanned, while Perl connects once to each switch and can read multiple tables out during a single connection. The question is, if I only want part of each table, is it faster to slurp a superset of data that I want and parse it down in the program, or to make the individual queries like I am currently doing?
  • the objects returned by the query (when you do a nextCmd) is a list; each list is composed of a single list; that list has two members, the OID, and the data. So if you are dealing with, say, tpFdbPortTable from above, you need to unpack it into the data like this:
for row in tpFdbPortTable:
        # each row is a list, which has a single list.
        (oid, destination) = row[0]
        # the target MAC is the last six components of the oid.
        target = ""
        for component in oid[-6:]:
            target = target + ':' + str(component)
        target = target[1:]
        print "Switch:", switch, "Target:", target, "Destination:", destination
  • I think the "correct" way to do this is to use the dictresult() function; but I can't for the life of me figure out how it would be more compact to use it.
no comments | post comment
This is a collection of techical information, much of it learned the hard way. Consider it a lab book or a /info directory. I doubt much of it will be of use to anyone else.

Useful:


snipsnap.org | Copyright 2000-2002 Matthias L. Jugel and Stephan J. Schmidt