pysnmp无法从不同的机器接收陷阱

时间:2018-05-08 15:48:34

标签: python-2.7 pysnmp

我没有本地主机IP,我有我的VM ip(eth0-192.168.12.20)来接收陷阱通知,如果从VM外部生成一个(我从其他机器使用snmptrap命令),我没有收到任何陷阱但是我我在VM接口eth0上执行tcpdump时可以看到SNMP数据。 如果我使用snmptrap命令从同一台机器生成陷阱,我可以通过PySNMP陷阱接收器脚本查看陷阱数据。

选项尝试: 1.尝试将端口绑定到0.0.0.0以从任何计算机接收陷阱 2.在pysnmp中启用调试选项以获得解决问题的一些想法。从外部机器发送snmptrap时没有生成信息

与我的问题类似的

The closest scenario出现在以下链接中,该链接没有最终解决方案。

代码: SNMP v1和v2c:

from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher
from pysnmp.carrier.asynsock.dgram import udp, udp6
from pyasn1.codec.ber import decoder
from pysnmp.proto import api
from pysnmp.entity import engine, config
from pysnmp.entity.rfc3413 import ntfrcv

from pysnmp import debug
debug.setLogger(debug.Debug("all"))

### SNMPv2c/SNMPv1 setup
### Callback function for receiving notifications
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()))


# 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(('0.0.0.0', 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

提前致谢

1 个答案:

答案 0 :(得分:1)

我在IT团队的帮助下发现了这个问题。基本上,API工作正常。

firewalld应用程序不允许数据包通过。因此,在我将SNMP端口添加到防火墙例外列表后,它使我的代码正常工作。

我使用的命令:

  • sudo firewall-cmd --add-port = 161-162 / udp --zone = public --permanent
  • sudo systemctl restart network
  • sudo systemctl reload firewalld