I have some equipment that can send snmp traps for alarms etc. I need to simulate this equipment for testing purpose. The output from the tester must be exactly like the output from the equipment but I need to control values etc. I have created a Python script using pysnmp and sendNotification for this. It seems to work fine. However the DISMAN-EVENT-MIB::sysUpTimeInstance is send automaticaly by sendNotification and the uptime is always 0. How can I set this value to something else? I know I can put an extra object in the addVarBinds but this will just give an extra object with another value. The initial sysUpTimeInstance is still 0. pysnmp 4.4.12 pysnmp-pyasn1 1.1.3 pysnmp-pysmi 1.1.10 pysnmplib 5.0.21
from pysnmp.hlapi import *
iterator7 = sendNotification(
SnmpEngine(),
CommunityData('public', mpModel=1),
UdpTransportTarget(('localhost', 162)),
ContextData(),
'trap',
NotificationType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB', 'netSnmpExampleNotification')).addVarBinds(ObjectType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB','netSnmpExampleHeartbeatRate'), 1))
)
errorIndication, errorStatus, errorIndex, varBinds = next(iterator7)
if errorIndication:
print(errorIndication)
The object used in the above are just examples. I have a another MIB file with the correct objects.
The above example will result in the following received:
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (0) 0:00:00.00 SNMPv2-MIB::snmpTrapOID.0 = OID: NET-SNMP-EXAMPLES-MIB::netSnmpExampleNotification NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatRate = INTEGER: 1
I am receiving snmp traps with snmptrapd
If you are using pysnmp-lextudio
package, the following code should work,
import asyncio
from pysnmp.hlapi.asyncio import *
async def run():
snmpEngine = SnmpEngine()
# Example of how you might update sysUpTime
mibBuilder = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder
sysUpTime, = mibBuilder.importSymbols('__SNMPv2-MIB', 'sysUpTime')
sysUpTime.syntax = TimeTicks(12345) # Set uptime to 12345
trap_result = await sendNotification(
snmpEngine,
CommunityData('public', mpModel=1),
UdpTransportTarget(('localhost', 162)),
ContextData(),
"trap",
NotificationType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB', 'netSnmpExampleNotification')).addVarBinds(ObjectType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB','netSnmpExampleHeartbeatRate'), 1))
)
errorIndication, errorStatus, errorIndex, varBinds = await trap_result
if errorIndication:
print(errorIndication)
snmpEngine.transportDispatcher.closeDispatcher()
asyncio.run(run())
No guarantee if you are using other pysnmp
packages.