Re: [pysnmp-users] pysnmp / pyasn1 decode problem with multi-line traps
Brought to you by:
elie
From: Ilya E. <il...@gl...> - 2015-03-12 13:40:01
|
Oh, if you are using SNMPv1 you should check against the base class: from pyasn1.type.univ import OctetString if isinstance(oval, OctetString): ... Perhaps this should be improved in pysnmp. -ilya On 03/12/2015 04:29 PM, Tom Kr. wrote: > I made the following changes to the script. > > ... > from pysnmp.proto.rfc1902 import OctetString > > def pySnmpCheck(oval): > if isinstance(oval, OctetString): > print("DEBUG: OctetString") > val = oval.asOctets() > else: > print("DEBUG: else") > val = oval > return val > > ... > for oid, val in varBinds: > val = pySnmpCheck(val) > print('%s = %s' % (oid.prettyPrint(), val.prettyPrint())) > ... > > Then I start grabbing the snmp messages again. > Unfortunately every value of the message go through the else part. > But how could this happen? > > Is there a possibility to check which type of string the values have? > > Tom > > > >> Am 11.03.2015 um 14:17 schrieb Ilya Etingof <il...@gl...>: >> >> >> That's right, .asOctets() is only defined for OctetString type and its >> subclasses. Therefore, to deal with octets of OctetString() you should >> do additional check: >> >> from pysnmp.proto.rfc1902 import OctetString >> >> ... >> >> if isinstance(val, OctetString): >> val = val.asOctets() >> >> Keep in mind that octet string is not always the same as printable >> string. The former is not immune to your terminal potentially eating >> non-printable characters or garbling output in a funny way. Also, text >> encoding is not defined by OctetString. To be on a safe side default >> OctetString printout is done in hex whenever a non-printable is seen in >> its contents. >> >> -ilya >> >>> On 03/11/2015 03:18 PM, Craig Small wrote: >>>> On Wed, Mar 11, 2015 at 07:02:44AM +0100, Tom wrote: >>>> ;AttributeError: ObjectSyntax instance has no attribute 'asOctets' >>> Are you sure each row is returning something asOctets makes sense for? >>> An Integer doesn't make sense. >>> >>>> Does it only works if a hex value is send by snmp? >>> I believe it works for certain types of values, such as an OctetString >>> >>>> How can I check if the value is hex or not? >>> Not sure, but the types are found in pyasn1.type.univ so you could >>> compare it to, say, univ.OctetString >>> >>> ObjectSyntax seems to be the superset of all types, from what I can tell >>> it means pysnmp wasn't sure what the type should be. >>> >>> It's been an interesting and educational discussion anyhow. >>> >>> - Craig >> |