PySNMP Not Able To Receive SNMPv2 Traps On Same Machine

247 Views Asked by At

Currently trying to implement a way to receive traps on a Python application. My device sends his snmpV2 traps to my machine directly(tested working with other software), but PySNMP doesn't seem to receive them. I'm using the example code directly from the documentation. My device is non-secured and doesn't need any password or certificates. Community is public.

I tried to change the binding port to use 0.0.0.0, 127.0.0.1 and my computer's IP directly. Still nothing.

Here's the code I'm using:

def v2cv1CallBackFunc(snmpEngine, stateReference, contextEngineId, contextName,
                      varBinds, cbCtx):
    transportDomain, transportAddress = snmpEngine.msgAndPduDsp.getTransportInfo(stateReference)
    print(transportDomain, transportAddress)
    # Get an execution context...
    execContext = snmpEngine.observer.getExecutionContext(
        'rfc3412.receiveMessage:request'
    )

    # ... and use inner SNMP engine data to figure out peer address
    print('Notification from %s, ContextEngineId "%s", ContextName "%s"'
                          %('@'.join([str(x) for x in execContext['transportAddress']]),
                            contextEngineId.prettyPrint(), contextName.prettyPrint()))

    for name, val in varBinds:
        print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))

def runSNMP():
    # Create SNMP engine with autogenernated engineID and pre-bound
    # to socket transport dispatcher
    snmpEngine = engine.SnmpEngine()

    # SNMPv1/2c setup
    # SecurityName <-> CommunityName mapping
    config.addV1System(snmpEngine, 'my-area', "public")

    # Specify security settings per SecurityName (SNMPv2c -> 1)
    config.addTargetParams(snmpEngine, 'my-creds', 'my-area', 'noAuthNoPriv', 1)

    # Transport setup
    # UDP over IPv4, first listening interface/port
    config.addSocketTransport(
        snmpEngine,
        udp.domainName + (1, ),
        udp.UdpSocketTransport().openServerMode(('127.0.0.1', 162))
    )

    # Register SNMP Application at the SNMP engine
    ntfrcv.NotificationReceiver(snmpEngine, v2cv1CallBackFunc)

    snmpEngine.transportDispatcher.jobStarted(1)  # this job would never finish

    # Run I/O dispatcher which would receive queries and send confirmations
    try:
        snmpEngine.transportDispatcher.runDispatcher()
    except:
        snmpEngine.transportDispatcher.closeDispatcher()
        raise```
0

There are 0 best solutions below