I am trying to build an SNMP agent based on PySNMP. This agent should both respond to commands received from the NMS and send traps to the NMS.
However, I can only get one of those functions to work at a time. Whenever I send a trap, commands from the NMS no longer work and I receive a timeout error. When the part of the code sending traps is deactivated, command handling works just fine.
It seems the issue stems from SnmpV1SecurityModel._com2sec(), where this part (L306-313) raises the error:
if communityName in self.__communityToTagMap:
if transportInformation in self.__transportToTagMap:
tags = self.__transportToTagMap[transportInformation].intersection(
self.__communityToTagMap[communityName])
elif self.__emptyTag in self.__communityToTagMap[communityName]:
tags = [self.__emptyTag]
else:
raise error.StatusInformation(errorIndication=errind.unknownCommunityName)
When running WITH traps, transportInformation is ((1, 3, 6, 1, 6, 1, 1), ('127.0.0.1', 59937)) while self.__transportToTagMap contains the key ((1, 3, 6, 1, 6, 1, 1), ('127.0.0.1', 40162)).
This means that once a trap is sent to the NMS at 127.0.0.1:40162, that is the only remote address that can be communicated through.
The command origination from the NMS at 127.0.0.1:59937 (random port) therefore does not produce a match and is rejected.
When running WITHOUT traps, self.__transportToTagMap is empty and self.__communityToTagMap[communityName] only contains the emptyTag.
This results in the tags being set to emptyTag and the processing being continued.
I am trying to build an SNMP agent based on PySNMP. This agent should both respond to commands received from the NMS and send traps to the NMS.
However, I can only get one of those functions to work at a time. Whenever I send a trap, commands from the NMS no longer work and I receive a timeout error. When the part of the code sending traps is deactivated, command handling works just fine.
It seems the issue stems from SnmpV1SecurityModel._com2sec(), where this part (L306-313) raises the error:
When running WITH traps,
transportInformation
is((1, 3, 6, 1, 6, 1, 1), ('127.0.0.1', 59937))
whileself.__transportToTagMap
contains the key((1, 3, 6, 1, 6, 1, 1), ('127.0.0.1', 40162))
. This means that once a trap is sent to the NMS at 127.0.0.1:40162, that is the only remote address that can be communicated through. The command origination from the NMS at 127.0.0.1:59937 (random port) therefore does not produce a match and is rejected.When running WITHOUT traps,
self.__transportToTagMap
is empty andself.__communityToTagMap[communityName]
only contains theemptyTag
. This results in the tags being set toemptyTag
and the processing being continued.Can anybody point me to what I am doing wrong?
Debug output WITH traps
``` 2021-08-18 11:09:41,358 pysnmp: sendVarBinds: final varBinds [(Debug output WITHOUT traps
``` 2021-08-18 11:08:31,862 pysnmp: receiveMessage: msgVersion 1, msg decoded 2021-08-18 11:08:31,862 pysnmp: prepareDataElements: Message: version=version-2c community=public data=PDUs: get-next-request=GetNextRequestPDU: request-id=384 error-status=noError error-index=0 variable-bindings=VarBindList: VarBind: name=1.1.1234.2.1.1.1.2 =_BindValue: unSpecified= 2021-08-18 11:08:31,862 pysnmp: _com2sec: built transport-to-tag map version 0: {} 2021-08-18 11:08:31,862 pysnmp: _com2sec: built securityName to securityModel map, version 0: {} 2021-08-18 11:08:31,863 pysnmp: _com2sec: built communityName to tag map (securityModel 2), version 4: {Crosspost on StackOverflow: https://stackoverflow.com/q/68830377/9211114