fileinsert
fileinsert

Reputation: 430

pysnmp not handling large string returns

I am trying to walk the sysORTable using the bulkget commandgenerator using the following code based from the samples:

cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.bulkCmd(
    cmdgen.UsmUserData(user, 
                    authKey=authKey, 
                    privKey=privKey, 
                    authProtocol=authProto, 
                    privProtocol=privProto, 
                    securityEngineId=None
            ),
    cmdgen.UdpTransportTarget((sHost, 161)),
    0 , 25, 
    *[cmdgen.MibVariable(oid) for oid in sOID] )

However the results returned from the agent are over the 255 character limit imposed by the MIB lookup. I have found two workarounds to this problem:

  1. Change the value of the max length of DisplayString in pysnmp/smi/mibs/SNMPv2-TC.py: subtypeSpec = OctetString.subtypeSpec + ValueSizeConstraint(0, 512)
  2. Disabling MIB lookups in the cmdgen with lookupMib=False

However both these fixes, while allowing the script to complete, seem to truncate the output. For example:

[ObjectType(ObjectIdentity(<ObjectName value object at 0x7f1c04686cd0 tagSet <TagSet object at 0x7f1c0c88dad0 tags 0:0:6> payload [1.3.6.1.2.1.1.9.1.3.106]>), <DisplayString value object at 0x7f1c04623150 subtypeSpec <ConstraintsIntersection object at 0x7f1c04a64490 consts <ValueSizeConstraint object at 0x7f1c0756c510 consts 0, 65535>, <ValueSizeConstraint object at 0x7f1c04a64450 consts 0, 512>> tagSet <TagSet object at 0x7f1c0c88d5d0 tags 0:0:4> encoding iso-8859-1 payload [Agent capabiliti...B
File name: sys]>)]

Note the ellipsis and the line break.

Two questions:

  1. How do I fix the truncating of output?
  2. What format is this message in and how do I unpack it? (quite different from a standard get output with a key and a value)

Upvotes: 0

Views: 623

Answers (1)

Ilya Etingof
Ilya Etingof

Reputation: 5555

First of, this seems like a bug in your SNMP agent - they should not overflow the string. In that sense pysnmp is doing just fine. ;-)

To answer your questions:

  1. The ellipsis is only present in repr(), it won't happen if you do str or .prettyPrint() on the value
  2. Essentially, it a sequence of tuples. Each tuple is (name, value). So to print stuff out you could do this:

:

for varBind in varBinds:
    print(' = '.join([x.prettyPrint() for x in varBind]))

The example can be found here.

Upvotes: 1

Related Questions