Skip to content

Commit

Permalink
restore snmp varbind error values
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-sahlmann committed Jul 31, 2023
1 parent e0c0e59 commit 04fc3b0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
21 changes: 12 additions & 9 deletions mb_netmgmt/snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,7 @@ def translate_json_to_network_response(self, json):
for oid, response in json.items():
if oid.startswith("_"):
continue
value = response["val"]
try:
value = b64decode(value, validate=True)
except (binascii.Error, TypeError):
pass
result += to_varbind(oid, value, response["tag"])
result += to_varbind(oid, response)
return result


Expand Down Expand Up @@ -143,6 +138,14 @@ def to_dict(varbind):
return result


def to_varbind(oid, value, tag):
asn1_class = ASN1_Class_UNIVERSAL.__dict__[tag]
return SNMPvarbind(oid=oid, value=asn1_class.asn1_object(value))
def to_varbind(oid, response):
value = response["val"]
try:
value = b64decode(value, validate=True)
asn1_class = ASN1_Class_UNIVERSAL.__dict__[response["tag"]]
value = asn1_class.asn1_object(value)
except (binascii.Error, TypeError):
pass
del response["val"]
del response["tag"]
return SNMPvarbind(oid=oid, value=value, **response)
9 changes: 8 additions & 1 deletion test/test_mb_netmgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,5 +262,12 @@ def test_no_such_instance_to_dict():


def test_to_varbind():
result = snmp.to_varbind("1.3", 0, "NULL")
result = snmp.to_varbind("1.3", response=dict(val=0, tag="NULL"))
assert result == SNMPvarbind(oid="1.3", value=0)


def test_to_varbind_no_such_instance():
result = snmp.to_varbind("1.3", response=dict(val=None, tag=None, noSuchInstance=0))
assert result == SNMPvarbind(
oid="1.3", value=None, noSuchObject=None, noSuchInstance=0, endOfMibView=None
)

0 comments on commit 04fc3b0

Please sign in to comment.