Listen traps with pysnmp

转载 2018年04月17日 14:23:41

Hello I'm trying to listen for traps with this code from pysnmp doc:

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

But when I test it with this command:

$ 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

Nothing is displayed. Could someone help me ? Everything I want is to display traps received by this receiver.

EDIT: I added some prints and this is what I get when I run the program:

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

When I send a trap, nothing displays. When I press Ctrl+C I get:

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

On Windows I found I had to change the listener address from 'localhost' to ''.

netstat -a then shows it bound as 0.0.0.0:162 instead of 127.0.0.0:162 and it works correctly:

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

PySNMP学习笔记(二)

原文地址:PySNMP学习笔记(二)作者:MichaelLing83关于getCmd的返回值。 The varBinds is a tuple of Managed Objects. Managed...
  • esonbest1234
  • esonbest1234
  • 2016-02-24 11:45:19
  • 354

PySNMP官方文档翻译---常用操作

常用操作在这个教程里,我们将会逐渐学习创建并运行一些不同的SNMP命令请求和通告。我们将使用PySNMP同步的使用最简单的高级API创建SNMP Engine在PySNMP里,SNMP Engine是...
  • u014793472
  • u014793472
  • 2016-09-23 17:34:59
  • 2415

pysnmp学习(一)

安装pysnmp: 编译安装的话,pysnmp需要用到asn.1,安装pysnmp的话需要先安装asn.1,不过我没有安装成功。 也可以使用easy_install pysnmp安...
  • shangzhihaohao
  • shangzhihaohao
  • 2011-11-19 23:15:27
  • 7985

PySNMP--GET command

GET commandpysnmp.hlapi.getCmd(snmpEngine, authData, transportTarget, contextData, *varBinds, **opti...
  • u014793472
  • u014793472
  • 2017-02-23 10:25:53
  • 359

python利用pysnmp获取交换机级联接口

交换机里有一个arp表,利用arp表可以找到与其相连的交换机的mac地址,然后再从mac端口对应表里找出端口 def getTrunk(ip,port,agent,communication...
  • shangzhihaohao
  • shangzhihaohao
  • 2011-11-30 22:05:18
  • 5306

ubuntu14.04 使用pysnmp SNMP trap 接受

下载 安装pysnmp-4.2.5.tar.gz (md5) https://pypi.python.org/pypi/pysnmp/ from pysnmp.carrier.asynso...
  • cauckfgf
  • cauckfgf
  • 2015-07-02 10:59:38
  • 1262

PySNMP扩展snmp-trap

pysnmp是用python语言开发的snmp开源项目,感觉没有net-snmp强大。但如果你只开发snmp 客户端的 trap,那么用pysnmp还是很简单的。 1、安装下载pysnmp和pyasn...
  • sealyao
  • sealyao
  • 2009-09-12 10:18:00
  • 7190

pysnmp的安装

pysnmp的安装   pysnmp依赖两个第三方的python库: (1) ASN.1 : Structure of Management Information:管理信息结构 用于定义...
  • yshh126
  • yshh126
  • 2015-07-16 17:30:19
  • 856

PySNMP学习笔记(一)

转自:http://blog.sina.com.cn/s/blog_54ce569c01009ccb.html SNMP standard introduces a set of ASN.1 lan...
  • wujingwen1111
  • wujingwen1111
  • 2014-02-12 10:19:41
  • 947

pysnmp安装

pysnmp依赖两个第三方的python库: (1) ASN.1 : Structure of Management Information:管理信息结构 用于定义通过网络管理协议可访问的...
  • wujingwen1111
  • wujingwen1111
  • 2013-12-26 18:37:26
  • 1078
收藏助手
不良信息举报
您举报文章:Listen traps with pysnmp
举报原因:
原因补充:

(最多只允许输入30个字)