1

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```
Zac6749
  • 11
  • 2
  • So you are not going to get anyone to debug your code. What does "tested working with other software" mean? You'll have to be more specific to get any help. If it works with other trap receivers that means you need to put debug statements in your code. – Gambit Support Sep 24 '21 at 16:16
  • You will have to enable debug logging like hinted in https://github.com/etingof/pysnmp/issues/107#issuecomment-344382846 to see what exactly happened. – Lex Li Jan 23 '23 at 04:11

0 Answers0