【问题标题】:SNMP Receiver listening to public IP address using pySNMP使用 pySNMP 监听公共 IP 地址的 SNMP 接收器
【发布时间】:2018-05-03 03:12:22
【问题描述】:

我使用 pysnmp 发送 SNMP 陷阱,代码如下 (trap.py) source

注意:X.X.X.X是我电脑的公网ip地址

from pysnmp.hlapi import *
from pysnmp import debug

hostname = 'X.X.X.X' ##public IP address of receiver
debug.setLogger(debug.Debug('msgproc'))
next(sendNotification(SnmpEngine(),
     CommunityData('public'),
     UdpTransportTarget((hostname, 162)),
     ContextData(),
     'trap',
     # sequence of custom OID-value pairs
     [ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'), OctetString('my string')),
      ObjectType(ObjectIdentity('1.3.6.1.2.1.1.3.0'), Integer32(42))]))

我正在使用带有此代码的 pysnmp 接收它 (receiver.py) source

from pysnmp.entity import engine, config
from pysnmp.carrier.asyncore.dgram import udp
from pysnmp.entity.rfc3413 import ntfrcv

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

# Transport setup

# UDP over IPv4, first listening interface/port
config.addTransport(
    snmpEngine,
    udp.domainName + (1,),
    udp.UdpTransport().openServerMode(('X.X.X.X', 162))
)
# SNMPv1/2c setup

# SecurityName <-> CommunityName mapping
config.addV1System(snmpEngine, 'my-area', 'public')


# Callback function for receiving notifications
# noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal
def cbFun(snmpEngine, stateReference, contextEngineId, contextName,
          varBinds, cbCtx):
    print('Notification from ContextEngineId "%s", ContextName "%s"' % (contextEngineId.prettyPrint(),
                                                                        contextName.prettyPrint()))
    for name, val in varBinds:
        print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))


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

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

在receiver.py中,我得到一个错误

pysnmp.carrier.error.CarrierError: bind() for ('X.X.X.X', 162) 失败:[WinError 10049] 请求的地址在其 上下文

我希望能够在非本地发送 SNMP 陷阱。所以我正在用我的公共 IP 地址测试 test.py。一切都在本地工作(如果我使用 127.0.0.1)但不能使用公共 IP 地址。我应该安装其他 SNMP 软件来收听 trap.py 吗?

【问题讨论】:

    标签: python snmp pysnmp


    【解决方案1】:

    这个错误:

    pysnmp.carrier.error.CarrierError: bind() for ('X.X.X.X', 162) failed: [WinError 10049] The requested address is not valid in its context
    

    与 SNMP 或 pysnmp 无关。它来自 Windows TCP/IP 堆栈。确保:

    • X.X.X.X 地址作为本地 IP 接口出现在您运行接收器的机器上
    • Windows 用户权限允许您绑定到低于 1024 的端口(例如本例中的 162)。或者,您可以尝试绑定到非标准端口,例如1024+ 看看是否有帮助。
    • Google for Windows 错误 10049,这是什么意思

    【讨论】:

    • 但是我的 traps.py 可以使用公共 IP 地址(不是本地 IP 地址)发送陷阱吗?我用whatismyipaddress.com作为IP地址,对吗?如何制作接收器?
    • 现在我正在使用来自云的 Ubuntu VM,它使用 snmptrapd 接收陷阱snmptrapd -CdfLo --disableAuthorization=yes。当我在这个 VM 上运行 traps.py 时,我能够接收到它。但是当我在另一台机器上运行它时,我不能。帮助?
    • 我猜你的 TRAP 接收脚本实际上应该使用本地 IP 而不是公共 IP。因为您的脚本绑定到必须是本地的特定 IP 接口。 TRAP 发起者和您的接收脚本之间的 NAT 设备会将网络地址从公共 IP 转换为私有(本地)IP。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多