From 27c1c8ef97a43f5b6446f09400d0a941564ffbcf Mon Sep 17 00:00:00 2001 From: elie Date: Sun, 31 May 2015 19:35:10 +0000 Subject: Major overhaul related to PySMI integration and Standard SNMP Applications API redesign: * MibVariable becomes ObjectIdentity and moves to pysnmp.smi.rfc1902 * ObjectType and NotificationType classes resempling corresponding MIB MACROs implemented * SNMP Standard Applications and examples modified to support ObjectType and NotificationType parameters --- .../multiple-different-notifications-at-once.py | 26 +++---- .../ntforg/send-inform-to-multiple-managers.py | 27 +++---- .../ntforg/send-notification-over-ipv4-and-ipv6.py | 27 +++---- .../ntforg/send-packet-from-specific-address.py | 22 ++---- .../agent/ntforg/send-trap-to-multiple-managers.py | 25 +++--- .../v3arch/asyncore/agent/ntforg/usm-md5-none.py | 24 ++---- examples/v3arch/asyncore/agent/ntforg/v1-trap.py | 25 +++--- .../v3arch/asyncore/agent/ntforg/v2c-inform.py | 28 +++---- .../agent/ntforg/v2c-trap-via-notification-type.py | 83 ++++++++------------ examples/v3arch/asyncore/agent/ntforg/v2c-trap.py | 27 +++---- examples/v3arch/asyncore/agent/ntforg/v3-trap.py | 25 +++--- .../getnext-multiple-oids-and-resolve-with-mib.py | 88 ++++++++++++++++++++++ 12 files changed, 220 insertions(+), 207 deletions(-) create mode 100644 examples/v3arch/asyncore/manager/cmdgen/getnext-multiple-oids-and-resolve-with-mib.py (limited to 'examples/v3arch') diff --git a/examples/v3arch/asyncore/agent/ntforg/multiple-different-notifications-at-once.py b/examples/v3arch/asyncore/agent/ntforg/multiple-different-notifications-at-once.py index a3fbb8d5..8f70ef9e 100644 --- a/examples/v3arch/asyncore/agent/ntforg/multiple-different-notifications-at-once.py +++ b/examples/v3arch/asyncore/agent/ntforg/multiple-different-notifications-at-once.py @@ -18,7 +18,7 @@ # from pysnmp.entity import engine, config from pysnmp.carrier.asynsock.dgram import udp -from pysnmp.entity.rfc3413 import ntforg, context +from pysnmp.entity.rfc3413 import ntforg from pysnmp.proto.api import v2c # Create SNMP engine instance @@ -80,9 +80,6 @@ config.addVacmUser(snmpEngine, 3, 'usr-md5-none', 'authNoPriv', (), (), (1,3,6)) # Create Notification Originator App instance. ntfOrg = ntforg.NotificationOriginator() -# Create default SNMP context where contextEngineId == SnmpEngineId -snmpContext = context.SnmpContext(snmpEngine) - # Error/confirmation receiver def cbFun(snmpEngine, sendRequestHandle, errorIndication, errorStatus, errorIndex, varBinds, cbCtx): @@ -94,18 +91,15 @@ def cbFun(snmpEngine, sendRequestHandle, errorIndication, # Build and submit notification message to dispatcher sendRequestHandle = ntfOrg.sendVarBinds( snmpEngine, - # Notification targets - 'my-notification', - # SNMP Context - snmpContext, - # contextName - '', - # notification name (SNMPv2-MIB::coldStart) - (1,3,6,1,6,3,1,1,5,1), - # instance Index - None, - # additional var-binds: ( (oid, value), ... ) - [ ((1,3,6,1,2,1,1,1,0), v2c.OctetString('Example Notificator')) ], + 'my-notification', # notification targets + None, '', # contextEngineId, contextName + # var-binds + [ + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::coldStart + ((1,3,6,1,6,3,1,1,4,1,0), v2c.ObjectIdentifier((1,3,6,1,6,3,1,1,5,1))), + # additional var-binds: ( (oid, value), ... ) + ((1,3,6,1,2,1,1,1,0), v2c.OctetString('Example Notificator')) + ], cbFun ) diff --git a/examples/v3arch/asyncore/agent/ntforg/send-inform-to-multiple-managers.py b/examples/v3arch/asyncore/agent/ntforg/send-inform-to-multiple-managers.py index 329268f1..709e9720 100644 --- a/examples/v3arch/asyncore/agent/ntforg/send-inform-to-multiple-managers.py +++ b/examples/v3arch/asyncore/agent/ntforg/send-inform-to-multiple-managers.py @@ -16,7 +16,7 @@ # from pysnmp.entity import engine, config from pysnmp.carrier.asynsock.dgram import udp -from pysnmp.entity.rfc3413 import ntforg, context +from pysnmp.entity.rfc3413 import ntforg from pysnmp.proto.api import v2c # Create SNMP engine instance @@ -74,25 +74,20 @@ config.addVacmUser(snmpEngine, 2, 'my-area', 'noAuthNoPriv', (), (), (1,3,6)) # Create Notification Originator App instance. ntfOrg = ntforg.NotificationOriginator() -# Create default SNMP context where contextEngineId == SnmpEngineId -snmpContext = context.SnmpContext(snmpEngine) - # Build and submit notification message to dispatcher ntfOrg.sendVarBinds( snmpEngine, # Notification targets - 'my-notification', - # SNMP Context - snmpContext, - # contextName - '', - # notification name (SNMPv2-MIB::coldStart) - (1,3,6,1,6,3,1,1,5,1), - # instance Index - None, - # additional var-binds: ( (oid, value), ... ) - [ ((1,3,6,1,2,1,1,1,0), v2c.OctetString('Example Notificator')), - ((1,3,6,1,2,1,1,5,0), v2c.OctetString('Notificator Example')) ] + 'my-notification', # notification targets + None, '', # contextEngineId, contextName + # var-binds + [ + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::coldStart + ((1,3,6,1,6,3,1,1,4,1,0), v2c.ObjectIdentifier((1,3,6,1,6,3,1,1,5,1))), + # additional var-binds: ( (oid, value), ... ) + ((1,3,6,1,2,1,1,1,0), v2c.OctetString('Example Notificator')), + ((1,3,6,1,2,1,1,5,0), v2c.OctetString('Notificator Example')) + ] ) print('Notifications are scheduled to be sent') diff --git a/examples/v3arch/asyncore/agent/ntforg/send-notification-over-ipv4-and-ipv6.py b/examples/v3arch/asyncore/agent/ntforg/send-notification-over-ipv4-and-ipv6.py index ea204f2c..e2589da7 100644 --- a/examples/v3arch/asyncore/agent/ntforg/send-notification-over-ipv4-and-ipv6.py +++ b/examples/v3arch/asyncore/agent/ntforg/send-notification-over-ipv4-and-ipv6.py @@ -15,7 +15,7 @@ # from pysnmp.entity import engine, config from pysnmp.carrier.asynsock.dgram import udp, udp6 -from pysnmp.entity.rfc3413 import ntforg, context +from pysnmp.entity.rfc3413 import ntforg from pysnmp.proto.api import v2c # Create SNMP engine instance @@ -73,25 +73,20 @@ config.addVacmUser(snmpEngine, 2, 'my-area', 'noAuthNoPriv', (), (), (1,3,6)) # Create Notification Originator App instance. ntfOrg = ntforg.NotificationOriginator() - # Create default SNMP context where contextEngineId == SnmpEngineId -snmpContext = context.SnmpContext(snmpEngine) - # Build and submit notification message to dispatcher ntfOrg.sendVarBinds( snmpEngine, # Notification targets - 'my-notification', - # SNMP Context - snmpContext, - # contextName - '', - # notification name (SNMPv2-MIB::coldStart) - (1,3,6,1,6,3,1,1,5,1), - # instance Index - None, - # additional var-binds: ( (oid, value), ... ) - [ ((1,3,6,1,2,1,1,1,0), v2c.OctetString('Example Notificator')), - ((1,3,6,1,2,1,1,5,0), v2c.OctetString('Notificator Example')) ] + 'my-notification', # notification targets + None, '', # contextEngineId, contextName + # var-binds + [ + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::coldStart + ((1,3,6,1,6,3,1,1,4,1,0), v2c.ObjectIdentifier((1,3,6,1,6,3,1,1,5,1))), + # additional var-binds: ( (oid, value), ... ) + ((1,3,6,1,2,1,1,1,0), v2c.OctetString('Example Notificator')), + ((1,3,6,1,2,1,1,5,0), v2c.OctetString('Notificator Example')) + ] ) print('Notification is scheduled to be sent') diff --git a/examples/v3arch/asyncore/agent/ntforg/send-packet-from-specific-address.py b/examples/v3arch/asyncore/agent/ntforg/send-packet-from-specific-address.py index 2bfa05d6..04dcd569 100644 --- a/examples/v3arch/asyncore/agent/ntforg/send-packet-from-specific-address.py +++ b/examples/v3arch/asyncore/agent/ntforg/send-packet-from-specific-address.py @@ -13,7 +13,7 @@ # from pysnmp.entity import engine, config from pysnmp.carrier.asynsock.dgram import udp -from pysnmp.entity.rfc3413 import ntforg, context +from pysnmp.entity.rfc3413 import ntforg from pysnmp.proto.api import v2c # Create SNMP engine instance @@ -57,22 +57,16 @@ config.addVacmUser(snmpEngine, 1, 'my-area', 'noAuthNoPriv', (), (), (1,3,6)) # Create Notification Originator App instance. ntfOrg = ntforg.NotificationOriginator() - # Create default SNMP context where contextEngineId == SnmpEngineId -snmpContext = context.SnmpContext(snmpEngine) - # Build and submit notification message to dispatcher ntfOrg.sendVarBinds( snmpEngine, - # Notification targets - 'my-notification', - # SNMP Context - snmpContext, - # contextName - '', - # notification name (SNMPv2-MIB::coldStart) - (1,3,6,1,6,3,1,1,5,1), - # instance Index - None + 'my-notification', # notification targets + None, '', # contextEngineId, contextName + # var-binds + [ + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::coldStart + ((1,3,6,1,6,3,1,1,4,1,0), v2c.ObjectIdentifier((1,3,6,1,6,3,1,1,5,1))) + ] ) print('Notification is scheduled to be sent') diff --git a/examples/v3arch/asyncore/agent/ntforg/send-trap-to-multiple-managers.py b/examples/v3arch/asyncore/agent/ntforg/send-trap-to-multiple-managers.py index 0ca3d702..cd6432b5 100644 --- a/examples/v3arch/asyncore/agent/ntforg/send-trap-to-multiple-managers.py +++ b/examples/v3arch/asyncore/agent/ntforg/send-trap-to-multiple-managers.py @@ -16,7 +16,7 @@ # from pysnmp.entity import engine, config from pysnmp.carrier.asynsock.dgram import udp -from pysnmp.entity.rfc3413 import ntforg, context +from pysnmp.entity.rfc3413 import ntforg from pysnmp.proto.api import v2c # Create SNMP engine instance @@ -70,22 +70,23 @@ config.addVacmUser(snmpEngine, 2, 'my-area', 'noAuthNoPriv', (), (), (1,3,6)) # *** SNMP engine configuration is complete by this line *** -# Create default SNMP context where contextEngineId == SnmpEngineId -snmpContext = context.SnmpContext(snmpEngine) - # Create Notification Originator App instance. -ntfOrg = ntforg.NotificationOriginator(snmpContext) +ntfOrg = ntforg.NotificationOriginator() # Build and submit notification message to dispatcher -ntfOrg.sendNotification( +ntfOrg.sendVarBinds( snmpEngine, # Notification targets - 'my-notification', - # Trap OID (SNMPv2-MIB::coldStart) - (1,3,6,1,6,3,1,1,5,1), - # ( (oid, value), ... ) - ( ((1,3,6,1,2,1,1,1,0), v2c.OctetString('Example Notificator')), - ((1,3,6,1,2,1,1,5,0), v2c.OctetString('Notificator Example')) ) + 'my-notification', # notification targets + None, '', # contextEngineId, contextName + # var-binds + [ + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::coldStart + ((1,3,6,1,6,3,1,1,4,1,0), v2c.ObjectIdentifier((1,3,6,1,6,3,1,1,5,1))), + # additional var-binds: ( (oid, value), ... ) + ((1,3,6,1,2,1,1,1,0), v2c.OctetString('Example Notificator')), + ((1,3,6,1,2,1,1,5,0), v2c.OctetString('Notificator Example')) + ] ) print('Notifications are scheduled to be sent') diff --git a/examples/v3arch/asyncore/agent/ntforg/usm-md5-none.py b/examples/v3arch/asyncore/agent/ntforg/usm-md5-none.py index facebe34..ef5355ad 100644 --- a/examples/v3arch/asyncore/agent/ntforg/usm-md5-none.py +++ b/examples/v3arch/asyncore/agent/ntforg/usm-md5-none.py @@ -13,7 +13,7 @@ # from pysnmp.entity import engine, config from pysnmp.carrier.asynsock.dgram import udp -from pysnmp.entity.rfc3413 import ntforg, context +from pysnmp.entity.rfc3413 import ntforg from pysnmp.proto.api import v2c # Create SNMP engine instance @@ -54,11 +54,8 @@ config.addVacmUser(snmpEngine, 3, 'usr-md5-none', 'authNoPriv', (), (), (1,3,6)) # *** SNMP engine configuration is complete by this line *** -# Create default SNMP context where contextEngineId == SnmpEngineId -snmpContext = context.SnmpContext(snmpEngine) - # Create Notification Originator App instance. -ntfOrg = ntforg.NotificationOriginator(snmpContext) +ntfOrg = ntforg.NotificationOriginator() # Error/confirmation receiver def cbFun(snmpEngine, sendRequestHandle, errorIndication, @@ -71,18 +68,11 @@ def cbFun(snmpEngine, sendRequestHandle, errorIndication, # Build and submit notification message to dispatcher sendRequestHandle = ntfOrg.sendVarBinds( snmpEngine, - # Notification targets - 'my-notification', - # SNMP Context - snmpContext, - # contextName - '', - # notification name (SNMPv2-MIB::coldStart) - (1,3,6,1,6,3,1,1,5,1), - # instance Index - None, - # additional var-binds: ( (oid, value), ... ) - [ ((1,3,6,1,2,1,1,5,0), v2c.OctetString('system name')) ], + 'my-notification', # notification targets + None, '', # contextEngineId, contextName + # var-binds: SNMPv2-MIB::coldStart, ... + [ ((1,3,6,1,6,3,1,1,5,1), v2c.ObjectIdentifier((1,3,6,1,6,3,1,1,5,1))), + ((1,3,6,1,2,1,1,5,0), v2c.OctetString('system name')) ], cbFun ) diff --git a/examples/v3arch/asyncore/agent/ntforg/v1-trap.py b/examples/v3arch/asyncore/agent/ntforg/v1-trap.py index f21bde50..2ac50718 100644 --- a/examples/v3arch/asyncore/agent/ntforg/v1-trap.py +++ b/examples/v3arch/asyncore/agent/ntforg/v1-trap.py @@ -17,7 +17,7 @@ # from pysnmp.entity import engine, config from pysnmp.carrier.asynsock.dgram import udp -from pysnmp.entity.rfc3413 import ntforg, context +from pysnmp.entity.rfc3413 import ntforg from pysnmp.proto.api import v2c #from pysnmp import debug @@ -64,28 +64,21 @@ config.addVacmUser(snmpEngine, 1, 'my-area', 'noAuthNoPriv', (), (), (1,3,6)) # Create Notification Originator App instance. ntfOrg = ntforg.NotificationOriginator() - # Create default SNMP context where contextEngineId == SnmpEngineId -snmpContext = context.SnmpContext(snmpEngine) - # Build and submit notification message to dispatcher ntfOrg.sendVarBinds( snmpEngine, # Notification targets - 'my-notification', - # SNMP Context - snmpContext, - # contextName - '', - # notification name: Generic Trap #6 (enterpriseSpecific) - # and Specific Trap 432 - '1.3.6.1.4.1.20408.4.1.1.2.0.432', - # instance Index - None, - # additional var-binds - [ + 'my-notification', # notification targets + None, '', # contextEngineId, contextName + # var-binds + [ # Uptime value with 12345 (v2c.ObjectIdentifier('1.3.6.1.2.1.1.3.0'), v2c.TimeTicks(12345)), + # trap OID: Generic Trap #6 (enterpriseSpecific) + # and Specific Trap 432 + (v2c.ObjectIdentifier('1.3.6.1.6.3.1.1.5.1'), + v2c.ObjectIdentifier('1.3.6.1.4.1.20408.4.1.1.2.0.432')), # Agent Address with '127.0.0.1' (v2c.ObjectIdentifier('1.3.6.1.6.3.18.1.3.0'), v2c.IpAddress('127.0.0.1')), diff --git a/examples/v3arch/asyncore/agent/ntforg/v2c-inform.py b/examples/v3arch/asyncore/agent/ntforg/v2c-inform.py index 08993b0e..9c52655d 100644 --- a/examples/v3arch/asyncore/agent/ntforg/v2c-inform.py +++ b/examples/v3arch/asyncore/agent/ntforg/v2c-inform.py @@ -15,7 +15,7 @@ # from pysnmp.entity import engine, config from pysnmp.carrier.asynsock.dgram import udp -from pysnmp.entity.rfc3413 import ntforg, context +from pysnmp.entity.rfc3413 import ntforg from pysnmp.proto.api import v2c # Create SNMP engine instance @@ -59,9 +59,6 @@ config.addVacmUser(snmpEngine, 2, 'my-area', 'noAuthNoPriv', (), (), (1,3,6)) # Create Notification Originator App instance. ntfOrg = ntforg.NotificationOriginator() -# Create default SNMP context where contextEngineId == SnmpEngineId -snmpContext = context.SnmpContext(snmpEngine) - # Error/confirmation receiver def cbFun(snmpEngine, sendRequestHandle, errorIndication, errorStatus, errorIndex, varBinds, cbCtx): @@ -73,19 +70,16 @@ def cbFun(snmpEngine, sendRequestHandle, errorIndication, # Build and submit notification message to dispatcher sendRequestHandle = ntfOrg.sendVarBinds( snmpEngine, - # Notification targets - 'my-notification', - # SNMP Context - snmpContext, - # contextName - '', - # notification name (SNMPv2-MIB::coldStart) - (1,3,6,1,6,3,1,1,5,1), - # instance Index - None, - # additional var-binds: ( (oid, value), ... ) - [ ((1,3,6,1,2,1,1,1,0), v2c.OctetString('Example Notificator')), - ((1,3,6,1,2,1,1,5,0), v2c.OctetString('Notificator Example')) ], + 'my-notification', # notification targets + None, '', # contextEngineId, contextName + # var-binds + [ + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::coldStart + ((1,3,6,1,6,3,1,1,4,1,0), v2c.ObjectIdentifier((1,3,6,1,6,3,1,1,5,1))), + # additional var-binds: ( (oid, value), ... ) + ((1,3,6,1,2,1,1,1,0), v2c.OctetString('Example Notificator')), + ((1,3,6,1,2,1,1,5,0), v2c.OctetString('Notificator Example')) + ], cbFun ) diff --git a/examples/v3arch/asyncore/agent/ntforg/v2c-trap-via-notification-type.py b/examples/v3arch/asyncore/agent/ntforg/v2c-trap-via-notification-type.py index e4ac2e01..ad17348a 100644 --- a/examples/v3arch/asyncore/agent/ntforg/v2c-trap-via-notification-type.py +++ b/examples/v3arch/asyncore/agent/ntforg/v2c-trap-via-notification-type.py @@ -8,28 +8,24 @@ # * over IPv4/UDP # * send TRAP notification # * to a Manager at 127.0.0.1:162 -# * with TRAP ID ACCOUNTING-CONTROL-MIB::acctngFileFull as MIB symbol +# * with TRAP ID IF-MIB::ifLink as MIB symbol # -# The ACCOUNTING-CONTROL-MIB::acctngFileFull NOTIFICATION-TYPE implies -# including three other var-binds into the TRAP describing the incident -# occurred. These var-binds are: -# ACCOUNTING-CONTROL-MIB::acctngFileMaximumSize.0 -# ACCOUNTING-CONTROL-MIB::acctngFileNameSuffix.0 -# ACCOUNTING-CONTROL-MIB::acctngFileName.0 +# The IF-MIB::ifLink NOTIFICATION-TYPE implies including four other +# var-binds into the notification message describing the incident +# occurred. These var-binds are: +# IF-MIB::ifIndex."x" +# IF-MIB::ifAdminStatus."x" +# IF-MIB::ifOperStatus."x" +# IF-MIB::ifDescr."x" # -# To run this example make sure to have ACCOUNTING-CONTROL-MIB.py in -# search path. +# Where "x" is MIB table index (instance index). +# +# To run this example make sure to have IF-MIB.py in search path. # 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 - -# Create SNMP engine instance -snmpEngine = engine.SnmpEngine() - -# Create default SNMP context where contextEngineId == SnmpEngineId -snmpContext = context.SnmpContext(snmpEngine) +from pysnmp.entity.rfc3413 import ntforg +from pysnmp.smi import rfc1902, view # # Here we fill in some values for Managed Objects Instances (invoked @@ -37,33 +33,19 @@ snmpContext = context.SnmpContext(snmpEngine) # In real Agent app, these values should already be initialized during # Agent runtime. # +instanceIndex = (1,) +objects = { + ('IF-MIB', 'ifIndex'): instanceIndex[0], + ('IF-MIB', 'ifAdminStatus'): 'up', + ('IF-MIB', 'ifOperStatus'): 'down', + ('IF-MIB', 'ifDescr'): 'eth0' +} -mibInstrumCtl = snmpContext.getMibInstrum('') -( MibScalarInstance, ) = mibInstrumCtl.mibBuilder.importSymbols( - 'SNMPv2-SMI', - 'MibScalarInstance' - ) -( acctngFileFull, - acctngFileMaximumSize, - acctngFileNameSuffix, - acctngFileName ) = mibInstrumCtl.mibBuilder.importSymbols( - 'ACCOUNTING-CONTROL-MIB', - 'acctngFileFull', - 'acctngFileMaximumSize', - 'acctngFileNameSuffix', - 'acctngFileName' - ) - -mibInstrumCtl.mibBuilder.exportSymbols( - '__ACCOUNTING-CONTROL-MIB', - MibScalarInstance(acctngFileMaximumSize.name, (0,), acctngFileMaximumSize.syntax.clone(123)), - MibScalarInstance(acctngFileNameSuffix.name, (0,), acctngFileNameSuffix.syntax.clone('.log')), - MibScalarInstance(acctngFileName.name, (0,), acctngFileName.syntax.clone('mylogfile')), -) +# Create SNMP engine instance +snmpEngine = engine.SnmpEngine() -# -# End of Agent's Managed Object Instances initialization -# +# MIB view controller is used for MIB lookup purposes +mibViewController = view.MibViewController(snmpEngine.getMibBuilder()) # SecurityName <-> CommunityName mapping config.addV1System(snmpEngine, 'my-area', 'public', transportTag='all-my-managers') @@ -107,16 +89,13 @@ ntfOrg = ntforg.NotificationOriginator() # Build and submit notification message to dispatcher ntfOrg.sendVarBinds( snmpEngine, - # Notification targets - 'my-notification', - # SNMP Context - snmpContext, - # contextName - '', - # notification name - ('ACCOUNTING-CONTROL-MIB', 'acctngFileFull'), - # MIB scalar/table instances of NOTIFICATION-TYPE objects - (0,) + 'my-notification', # notification targets + None, '', # contextEngineId, contextName + rfc1902.NotificationType( + rfc1902.ObjectIdentity('IF-MIB', 'linkUp'), + instanceIndex=instanceIndex, + objects=objects + ).resolveWithMib(mibViewController) ) print('Notification is scheduled to be sent') diff --git a/examples/v3arch/asyncore/agent/ntforg/v2c-trap.py b/examples/v3arch/asyncore/agent/ntforg/v2c-trap.py index 4e3465b2..d63a371e 100644 --- a/examples/v3arch/asyncore/agent/ntforg/v2c-trap.py +++ b/examples/v3arch/asyncore/agent/ntforg/v2c-trap.py @@ -15,7 +15,7 @@ # from pysnmp.entity import engine, config from pysnmp.carrier.asynsock.dgram import udp -from pysnmp.entity.rfc3413 import ntforg, context +from pysnmp.entity.rfc3413 import ntforg from pysnmp.proto.api import v2c # Create SNMP engine instance @@ -58,9 +58,6 @@ config.addVacmUser(snmpEngine, 2, 'my-area', 'noAuthNoPriv', (), (), (1,3,6)) # Create Notification Originator App instance. ntfOrg = ntforg.NotificationOriginator() - # Create default SNMP context where contextEngineId == SnmpEngineId -snmpContext = context.SnmpContext(snmpEngine) - # Error/confirmation receiver def cbFun(snmpEngine, sendRequestHandle, errorIndication, errorStatus, errorIndex, varBinds, cbCtx): @@ -73,18 +70,16 @@ def cbFun(snmpEngine, sendRequestHandle, errorIndication, sendRequestHandle = ntfOrg.sendVarBinds( snmpEngine, # Notification targets - 'my-notification', - # SNMP Context - snmpContext, - # contextName - '', - # notification name (SNMPv2-MIB::coldStart) - (1,3,6,1,6,3,1,1,5,1), - # instance Index - None, - # additional var-binds: ( (oid, value), ... ) - [ ((1,3,6,1,2,1,1,1,0), v2c.OctetString('Example Notificator')), - ((1,3,6,1,2,1,1,5,0), v2c.OctetString('Notificator Example')) ], + 'my-notification', # notification targets + None, '', # contextEngineId, contextName + # var-binds + [ + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::coldStart + ((1,3,6,1,6,3,1,1,4,1,0), v2c.ObjectIdentifier((1,3,6,1,6,3,1,1,5,1))), + # additional var-binds: ( (oid, value), ... ) + ((1,3,6,1,2,1,1,1,0), v2c.OctetString('Example Notificator')), + ((1,3,6,1,2,1,1,5,0), v2c.OctetString('Notificator Example')) + ], cbFun ) diff --git a/examples/v3arch/asyncore/agent/ntforg/v3-trap.py b/examples/v3arch/asyncore/agent/ntforg/v3-trap.py index bff341a3..266d91ec 100644 --- a/examples/v3arch/asyncore/agent/ntforg/v3-trap.py +++ b/examples/v3arch/asyncore/agent/ntforg/v3-trap.py @@ -13,7 +13,7 @@ # from pysnmp.entity import engine, config from pysnmp.carrier.asynsock.dgram import udp -from pysnmp.entity.rfc3413 import ntforg, context +from pysnmp.entity.rfc3413 import ntforg from pysnmp.proto.api import v2c # Create SNMP engine instance with specific (and locally unique) @@ -62,24 +62,19 @@ config.addVacmUser(snmpEngine, 3, 'usr-md5-des', 'authPriv', (), (), (1,3,6)) # Create Notification Originator App instance. ntfOrg = ntforg.NotificationOriginator() - # Create default SNMP context where contextEngineId == SnmpEngineId -snmpContext = context.SnmpContext(snmpEngine) - # Build and submit notification message to dispatcher ntfOrg.sendVarBinds( snmpEngine, # Notification targets - 'my-notification', - # SNMP Context - snmpContext, - # contextName - '', - # notification name (SNMPv2-MIB::coldStart) - (1,3,6,1,6,3,1,1,5,1), - # instance Index - None, - # additional var-binds: ( (oid, value), ... ) - [ ((1,3,6,1,2,1,1,5,0), v2c.OctetString('system name')) ] + 'my-notification', # notification targets + None, '', # contextEngineId, contextName + # var-binds + [ + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::coldStart + ((1,3,6,1,6,3,1,1,4,1,0), v2c.ObjectIdentifier((1,3,6,1,6,3,1,1,5,1))), + # additional var-binds: ( (oid, value), ... ) + ((1,3,6,1,2,1,1,5,0), v2c.OctetString('Notificator Example')) + ] ) print('Notification is scheduled to be sent') diff --git a/examples/v3arch/asyncore/manager/cmdgen/getnext-multiple-oids-and-resolve-with-mib.py b/examples/v3arch/asyncore/manager/cmdgen/getnext-multiple-oids-and-resolve-with-mib.py new file mode 100644 index 00000000..a8c5b35b --- /dev/null +++ b/examples/v3arch/asyncore/manager/cmdgen/getnext-multiple-oids-and-resolve-with-mib.py @@ -0,0 +1,88 @@ +# +# GETNEXT Command Generator +# +# Send a series of SNMP GETNEXT requests +# with SNMPv1, community 'public' +# over IPv4/UDP +# to an Agent at 195.218.195.228:161 +# for two OIDs in tuple form +# stop on end-of-mib condition for both OIDs +# +# This script performs similar to the following Net-SNMP command: +# +# $ snmpwalk -v1 -c public -ObentU 195.218.195.228 1.3.6.1.2.1.1 1.3.6.1.4.1.1 +# +from pysnmp.entity import engine, config +from pysnmp.carrier.asynsock.dgram import udp +from pysnmp.entity.rfc3413 import cmdgen +from pysnmp.smi import compiler, view, rfc1902 + +# Create SNMP engine instance +snmpEngine = engine.SnmpEngine() + +# Attach MIB compiler to SNMP Engine (MIB Builder) +compiler.addMibCompiler(snmpEngine.getMibBuilder()) + +# Used for MIB objects resolution +mibViewController = view.MibViewController(snmpEngine.getMibBuilder()) + +# +# +# SNMPv1/2c setup +# + +# SecurityName <-> CommunityName mapping +config.addV1System(snmpEngine, 'my-area', 'public') + +# Specify security settings per SecurityName (SNMPv1 - 0, SNMPv2c - 1) +config.addTargetParams(snmpEngine, 'my-creds', 'my-area', 'noAuthNoPriv', 1) + +# +# Setup transport endpoint and bind it with security settings yielding +# a target name +# + +# UDP/IPv4 +config.addTransport( + snmpEngine, + udp.domainName, + udp.UdpSocketTransport().openClientMode() +) +config.addTargetAddr( + snmpEngine, 'my-router', + udp.domainName, ('195.218.195.228', 161), + 'my-creds' +) + +# Error/response receiver +def cbFun(snmpEngine, sendRequestHandle, errorIndication, + errorStatus, errorIndex, varBindTable, cbCtx): + if errorIndication: + print(errorIndication) + return + # SNMPv1 response may contain noSuchName error *and* SNMPv2c exception, + # so we ignore noSuchName error here + if errorStatus and errorStatus != 2: + print('%s at %s' % ( + errorStatus.prettyPrint(), + errorIndex and varBindTable[-1][int(errorIndex)-1][0] or '?' + ) + ) + return # stop on error + for varBindRow in varBindTable: + for varBind in varBindRow: + print(rfc1902.ObjectType(rfc1902.ObjectIdentity(varBind[0]), varBind[1]).resolveWithMib(mibViewController).prettyPrint()) + return 1 # signal dispatcher to continue + +# Prepare initial request to be sent +cmdgen.NextCommandGenerator().sendVarBinds( + snmpEngine, + 'my-router', + None, '', # contextEngineId, contextName + [ rfc1902.ObjectType(rfc1902.ObjectIdentity('iso.org.dod')).resolveWithMib(mibViewController), + rfc1902.ObjectType(rfc1902.ObjectIdentity('IF-MIB', 'ifMIB')).resolveWithMib(mibViewController) ], + cbFun +) + +# Run I/O dispatcher which would send pending queries and process responses +snmpEngine.transportDispatcher.runDispatcher() -- cgit v1.2.1