1

目前正在尝试实现一种在 Python 应用程序上接收陷阱的方法。我的设备直接将他的 snmpV2 陷阱发送到我的机器(测试与其他软件一起使用),但 PySNMP 似乎没有接收到它们。我直接使用文档中的示例代码。我的设备不安全,不需要任何密码或证书。社区是公开的。

我尝试将绑定端口更改为直接使用 0.0.0.0、127.0.0.1 和我的计算机的 IP。依然没有。

这是我正在使用的代码:

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```
4

0 回答 0