python - 使用 pysnmp 监听陷阱

标签 python pysnmp

你好,我正在尝试使用 pysnmp 文档中的这段代码来监听陷阱:

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

def cbFun(transportDispatcher, transportDomain, transportAddress, wholeMsg):
    print('cbFun is called')
    while wholeMsg:
    print('loop...')
        msgVer = int(api.decodeMessageVersion(wholeMsg))
        if msgVer in api.protoModules:
            pMod = api.protoModules[msgVer]
        else:
            print('Unsupported SNMP version %s' % msgVer)
            return
        reqMsg, wholeMsg = decoder.decode(
            wholeMsg, asn1Spec=pMod.Message(),
            )
        print('Notification message from %s:%s: ' % (
            transportDomain, transportAddress
            )
        )
        reqPDU = pMod.apiMessage.getPDU(reqMsg)
        if reqPDU.isSameTypeWith(pMod.TrapPDU()):
            if msgVer == api.protoVersion1:
                print('Enterprise: %s' % (
                    pMod.apiTrapPDU.getEnterprise(reqPDU).prettyPrint()
                    )
                )
                print('Agent Address: %s' % (
                    pMod.apiTrapPDU.getAgentAddr(reqPDU).prettyPrint()
                    )
                )
                print('Generic Trap: %s' % (
                    pMod.apiTrapPDU.getGenericTrap(reqPDU).prettyPrint()
                    )
                )
                print('Specific Trap: %s' % (
                    pMod.apiTrapPDU.getSpecificTrap(reqPDU).prettyPrint()
                    )
                )
                print('Uptime: %s' % (
                    pMod.apiTrapPDU.getTimeStamp(reqPDU).prettyPrint()
                    )
                )
                varBinds = pMod.apiTrapPDU.getVarBindList(reqPDU)
            else:
                varBinds = pMod.apiPDU.getVarBindList(reqPDU)
            print('Var-binds:')
            for oid, val in varBinds:
                print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))
    return wholeMsg

transportDispatcher = AsynsockDispatcher()

transportDispatcher.registerRecvCbFun(cbFun)

# UDP/IPv4
transportDispatcher.registerTransport(
    udp.domainName, udp.UdpSocketTransport().openServerMode(('localhost', 162))
)

# UDP/IPv6
transportDispatcher.registerTransport(
    udp6.domainName, udp6.Udp6SocketTransport().openServerMode(('::1', 162))
)

transportDispatcher.jobStarted(1)

try:
    # Dispatcher will never finish as job#1 never reaches zero
    print('run dispatcher')
    transportDispatcher.runDispatcher()
except:
    transportDispatcher.closeDispatcher()
    raise

但是当我用这个命令测试它时:

$ snmptrap -v1 -c public 127.0.0.1 1.3.6.1.4.1.20408.4.1.1.2
127.0.0.1 1 1 123 1.3.6.1.2.1.1.1.0 s test

什么都不显示。有人可以帮我吗? 我想要的只是显示此接收器收到的陷阱。

编辑:我添加了一些打印品,这是我运行程序时得到的:

C:\user\snmp\test>python fonction.py
rundispatcher
_

当我发送陷阱时,没有任何显示。当我按 Ctrl+C 时,我得到:

C:\user\snmp\test>python fonction.py
rundispatcher
Traceback (most recent call last):
  File "fonction.py", line 73, in <module>
    transportDispatcher.runDispatcher()
  File "C:\user\snmp\test\pysnmp\carrier\asyncore\dispatch.py", line 37, in runDispatcher
    use_poll=True, map=self.__sockMap, count=1)
  File "C:\Python27\lib\asyncore.py", line 220, in loop
    poll_fun(timeout, map)
  File "C:\Python27\lib\asyncore.py", line 145, in poll
    r, w, e = select.select(r, w, e, timeout)
KeyboardInterrupt

最佳答案

在 Windows 上,我发现我必须将监听器地址从“localhost”更改为“”。

netstat -a 然后显示它绑定(bind)为 0.0.0.0:162 而不是 127.0.0.0:162 并且它工作正常:

C:\>netstat -a | find ":162"
  UDP    0.0.0.0:162            *:*

关于python - 使用 pysnmp 监听陷阱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33752602/

相关文章:

python - 分配给 numpy 数组时避免 np.where

python - 是否有用于列出素数的 Python 库?

python - Django 内联表单集不会这样做

python - 将 snmp 八位字节字符串转换为人类可读的日期格式

ubuntu - 如何在 Ubuntu 中使用 snmp 检索 hrSWRuntable?

python - 使用 pysnmp 获取单个 SNMP 值

Python 和蓝牙/OBEX

python - 登录后无法打开新窗口

python - 是否可以从该表中提取接口(interface)名称和接口(interface)状态?