使用私有mibs与snmpv3

时间:2018-03-24 14:15:27

标签: pysnmp

我使用下面提到的示例发送带有自定义通知mib的v3陷阱

http://pysnmp.sourceforge.net/examples/current/v3arch/agent/ntforg/trap-v3.html

但是sendNotification函数只接受如示例中的虚线格式。

 What should I do to pass the symbols instead ?

例如('MY-MIB:testTrap')而不是(1,3,2,0 ....)

2 个答案:

答案 0 :(得分:0)

您正在链接历史pysnmp文档。请参阅actual one

要传递MIB符号,您应该使用ObjectIdentifier类,它将MIB名称转换为幕后的OID。

但SNMP通知可能非常复杂,因为它们可能意味着某些其他MIB对象会自动包含在发送的通知中。

但您仍然可以通过NotificationType.addVarBinds添加所需的任何MIB对象。

这是一个简单的例子:

from pysnmp.hlapi import *

errorIndication, errorStatus, errorIndex, varBinds = next(
    sendNotification(
        SnmpEngine(),
        CommunityData('public'),
        UdpTransportTarget(('demo.snmplabs.com', 162)),
        ContextData(),
        'trap',
        NotificationType(
            ObjectIdentity('MY-MIB', 'testTrap')
        )
    )
)

答案 1 :(得分:0)

"Hi again below is a snippet of the way I am using the code .  "

from pysnmp.entity import engine, config

from pysnmp.carrier.asynsock.dgram import udp

from pysnmp.entity.rfc3413 import ntforg, context

from pysnmp.proto.api import v2c

from pysnmp.proto import rfc1902

from pysnmp.smi import builder

file = open("."+"/file.txt","r")
line = file.read()
fields = line.split(",")
file.close()

file = open("."+"/credentials.txt","r")

cred = file.read()

fields = cred.split(",")

file.close()

snmpEngine = engine.SnmpEngine(snmpEngineID=v2c.OctetString(hexValue=cred[0]))

config.addV3User(snmpEngine, cred[1], config.usmHMACSHAAuthProtocol, cred[2], config.usmAesCfb128Protocol, cred[3])

config.addTargetParams(snmpEngine, 'my-creds', 'traptest', 'authPriv')

config.addSocketTransport(snmpEngine,  udp.domainName, udp.UdpSocketTransport().openClientMode())

config.addTargetAddr(snmpEngine, 'my-nms', udp.domainName, ('127.0.0.1', 162), 'my-creds', tagList='all-my-managers')

config.addNotificationTarget(snmpEngine, 'my-notification', 'my-filter', 'all-my-managers', 'trap')

config.addContext(snmpEngine, '')

config.addVacmUser(snmpEngine, 3, 'traptest', 'authPriv', (), (), (1,3,6))

snmpContext = context.SnmpContext(snmpEngine)

ntfOrg = ntforg.NotificationOriginator()

ntfOrg.snmpContext = snmpContext

ntfOrg.sendNotification(

    snmpEngine,

    'my-notification',

    (1,3,6,1,4,1,46033,1,1,2,2),

    (((1,3,6,1,4,1,46033,1,1,1,1), v2c.OctetString(fields[0])),)

)

print('Notification is scheduled to be sent')