I’m using pysnmp and encountering an issue with an SNMP MIB object IF-MIB::ifAlias while trying to cast its value to a DisplayString. The value is an OctetString and contains specific information encoded in ISO-8859-1.
Relevant code:
async def walk(self, oidstring: str) -> dict[str, str]:
oid = ObjectIdentity(oidstring)
varBinds = [ObjectType(oid)]
result: dict[str, str] = {}
while True:
errorIndication, errorStatus, _, varBindTable = await bulkCmd(
SnmpEngine(),
CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 161)),
ContextData(),
0, 50,
*varBinds,
maxCalls=1000,
lookupMib=False
)
if errorIndication:
if isinstance(errorIndication, RequestTimedOut):
raise Exception("SNMP request timed out")
raise Exception(errorIndication)
elif errorStatus:
raise Exception(errorStatus.prettyPrint())
else:
for varBindRow in varBindTable:
if oid.isPrefixOf(varBindRow[0][0]):
index, value = varBindRow[0]
result[str(index[len(oid):])] = str(value)
else:
return result
varBinds = varBindTable[-1]
Here is the error message I’m receiving:
MIB object 'IF-MIB::ifAlias.287' having type 'DisplayString' failed to cast value <OctetString
value object, tagSet <TagSet object, tags 0:0:4>, subtypeSpec <ConstraintsIntersection object,
consts <ValueSizeConstraint object, consts 0, 65535>>, encoding iso-8859-1, payload [REDACTED]>:
<ConstraintsIntersection object, consts <ValueSizeConstraint object, consts 0, 65535>,
<ValueSizeConstraint object, consts 0, 255>, <ValueSizeConstraint object, consts 0, 64>> failed at:
ValueConstraintError("<ValueSizeConstraint object, consts 0, 64> failed at:
ValueConstraintError(b'REDACTED')") at DisplayString caused by <class
'pyasn1.type.error.ValueConstraintError'>: <ConstraintsIntersection object, consts
<ValueSizeConstraint object, consts 0, 65535>, <ValueSizeConstraint object, consts 0, 255>,
<ValueSizeConstraint object, consts 0, 64>> failed at: ValueConstraintError("<ValueSizeConstraint
object, consts 0, 64> failed at: ValueConstraintError(b'REDACTED')") at DisplayString caused by
<class 'pysnmp.smi.error.SmiError'>
I suspect the ifAlias constraint at 64 characters is being violated by my SNMP agent, similar to the issue mentioned in this ticket on the pysnmp repo.
I’m not sure how to resolve this constraint issue, as altering the value of ifAlias on the SNMP agent is not an option. Could anyone guide me on:
How I can circumvent this issue and possibly disable checking the constraints in pysnmp?
Do I need to implement a custom MIB that doesn’t set the constraint on ifAlias?
Any help or pointers would be greatly appreciated!
mattias is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.