forked from etingof/pysnmp
-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
519 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-informs-at-once.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
""" | ||
Multiple concurrent notifications | ||
+++++++++++++++++++++++++++++++++ | ||
Send multiple SNMP notifications at once using the following options: | ||
* SNMPv2c and SNMPv3 | ||
* with community name 'public' | ||
* over IPv4/UDP | ||
* send INFORM notification | ||
* to multiple Managers | ||
* with TRAP ID 'coldStart' specified as a MIB symbol | ||
* include managed object information specified as var-bind objects pair | ||
Functionally similar to: | ||
| $ snmpinform -v2c -c public demo.pysnmp.com 123 1.3.6.1.6.3.1.1.5.1 | ||
| $ snmpinform -v3 -u usr-md5-des -l authPriv -A authkey1 -X privkey1 demo.pysnmp.com 123 1.3.6.1.6.3.1.1.5.1 | ||
""" # | ||
import asyncio | ||
from pysnmp.hlapi.v3arch.asyncio import * | ||
|
||
|
||
async def run(): | ||
# List of targets in the following format: | ||
# ( ( authData, transportTarget ), ... ) | ||
TARGETS = ( | ||
# 1-st target (SNMPv2c over IPv4/UDP) | ||
( | ||
CommunityData("public"), | ||
await UdpTransportTarget.create(("demo.pysnmp.com", 162)), | ||
ContextData(), | ||
), | ||
# 2-nd target (SNMPv3 over IPv4/UDP) | ||
( | ||
UsmUserData("usr-md5-des", "authkey1", "privkey1"), | ||
await UdpTransportTarget.create(("demo.pysnmp.com", 162)), | ||
ContextData(), | ||
), | ||
) | ||
|
||
snmpEngine = SnmpEngine() | ||
|
||
for authData, transportTarget, contextData in TARGETS: | ||
( | ||
errorIndication, | ||
errorStatus, | ||
errorIndex, | ||
varBindTable, | ||
) = await sendNotification( | ||
snmpEngine, | ||
authData, | ||
transportTarget, | ||
contextData, | ||
"inform", # NotifyType | ||
NotificationType(ObjectIdentity("SNMPv2-MIB", "coldStart")).addVarBinds( | ||
("1.3.6.1.2.1.1.1.0", "my name") | ||
), | ||
) | ||
|
||
if errorIndication: | ||
print("Notification not sent: %s" % errorIndication) | ||
elif errorStatus: | ||
print( | ||
"Notification Receiver returned error: %s @%s" | ||
% (errorStatus, errorIndex) | ||
) | ||
else: | ||
print("Notification delivered:") | ||
for name, val in varBindTable: | ||
print(f"{name.prettyPrint()} = {val.prettyPrint()}") | ||
|
||
snmpEngine.transportDispatcher.runDispatcher() | ||
|
||
|
||
asyncio.run(run()) |
66 changes: 0 additions & 66 deletions
66
examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-notifications-at-once.py
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-traps-at-once.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
""" | ||
Multiple concurrent notifications | ||
+++++++++++++++++++++++++++++++++ | ||
Send multiple SNMP notifications at once using the following options: | ||
* SNMPv2c and SNMPv3 | ||
* with community name 'public' | ||
* over IPv4/UDP | ||
* send TRAP notification | ||
* to multiple Managers | ||
* with TRAP ID 'coldStart' specified as a MIB symbol | ||
* include managed object information specified as var-bind objects pair | ||
Functionally similar to: | ||
| $ snmptrap -v1 -c public demo.pysnmp.com 1.3.6.1.4.1.20408.4.1.1.2 demo.pysnmp.com 6 432 12345 1.3.6.1.2.1.1.1.0 s "my system" | ||
| $ snmptrap -v2c -c public demo.pysnmp.com 123 1.3.6.1.6.3.1.1.5.1 | ||
""" # | ||
import asyncio | ||
from pysnmp.hlapi.v3arch.asyncio import * | ||
|
||
|
||
async def run(): | ||
# List of targets in the following format: | ||
# ( ( authData, transportTarget ), ... ) | ||
TARGETS = ( | ||
# 1-st target (SNMPv1 over IPv4/UDP) | ||
( | ||
CommunityData("public", mpModel=0), | ||
await UdpTransportTarget.create(("demo.pysnmp.com", 162)), | ||
ContextData(), | ||
), | ||
# 2-nd target (SNMPv2c over IPv4/UDP) | ||
( | ||
CommunityData("public"), | ||
await UdpTransportTarget.create(("demo.pysnmp.com", 162)), | ||
ContextData(), | ||
), | ||
) | ||
|
||
snmpEngine = SnmpEngine() | ||
|
||
for authData, transportTarget, contextData in TARGETS: | ||
await sendNotification( | ||
snmpEngine, | ||
authData, | ||
transportTarget, | ||
contextData, | ||
"trap", # NotifyType | ||
NotificationType(ObjectIdentity("SNMPv2-MIB", "coldStart")).addVarBinds( | ||
("1.3.6.1.2.1.1.1.0", "my name") | ||
), | ||
) | ||
|
||
snmpEngine.transportDispatcher.runDispatcher() | ||
|
||
|
||
asyncio.run(run()) |
110 changes: 110 additions & 0 deletions
110
examples/hlapi/v3arch/asyncio/agent/ntforg/running-multiple-snmp-engines-at-once.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
""" | ||
Multiple SNMP Engines | ||
+++++++++++++++++++++ | ||
Send SNMP notifications in behalf of multiple independent SNMP engines | ||
using the following options: | ||
* with a single transport dispatcher and two independent SNMP engines | ||
* SNMPv2c and SNMPv3 | ||
* with community name 'public' or USM username usr-md5-des | ||
* over IPv4/UDP | ||
* send INFORM notification | ||
* to multiple Managers | ||
* with TRAP ID 'coldStart' specified as a MIB symbol | ||
* include managed object information specified as var-bind objects pair | ||
Within this script we have a single asynchronous TransportDispatcher | ||
and a single UDP-based transport serving two independent SNMP engines. | ||
We use a single instance of AsyncNotificationOriginator with each of | ||
SNMP Engines to communicate INFORM notification to remote systems. | ||
When we receive a [response] message from remote system we use | ||
a custom message router to choose what of the two SNMP engines | ||
data packet should be handed over. The selection criteria we | ||
employ here is based on peer's UDP port number. Other selection | ||
criteria are also possible. | ||
| $ snmpinform -v2c -c public demo.pysnmp.com:1162 123 1.3.6.1.6.3.1.1.5.1 | ||
| $ snmpinform -v3 -u usr-md5-des -l authPriv -A authkey1 -X privkey1 demo.pysnmp.com 123 1.3.6.1.6.3.1.1.5.1 | ||
""" # | ||
import asyncio | ||
from pysnmp.hlapi.v3arch.asyncio import * | ||
from pysnmp.carrier.asyncio.dispatch import AsyncioDispatcher | ||
|
||
|
||
async def run(): | ||
# List of targets in the following format: | ||
# ( ( authData, transportTarget ), ... ) | ||
TARGETS = ( | ||
# 1-st target (SNMPv2c over IPv4/UDP) | ||
( | ||
CommunityData("public"), | ||
await UdpTransportTarget.create(("demo.pysnmp.com", 162)), # TODO: 1162 | ||
ContextData(), | ||
), | ||
# 2-nd target (SNMPv3 over IPv4/UDP) | ||
( | ||
UsmUserData("usr-md5-des", "authkey1", "privkey1"), | ||
await UdpTransportTarget.create(("demo.pysnmp.com", 162)), | ||
ContextData(), | ||
), | ||
) | ||
|
||
# Instantiate the single transport dispatcher object | ||
transportDispatcher = AsyncioDispatcher() | ||
|
||
# Setup a custom data routing function to select snmpEngine by transportDomain | ||
transportDispatcher.registerRoutingCbFun(lambda td, ta, d: ta[1] % 3 and "A" or "B") | ||
|
||
snmpEngineA = SnmpEngine() | ||
snmpEngineA.registerTransportDispatcher(transportDispatcher, "A") | ||
|
||
snmpEngineB = SnmpEngine() | ||
snmpEngineB.registerTransportDispatcher(transportDispatcher, "B") | ||
|
||
for authData, transportTarget, contextData in TARGETS: | ||
# Pick one of the two SNMP engines | ||
snmpEngine = ( | ||
transportTarget.getTransportInfo()[1][1] % 3 and snmpEngineA or snmpEngineB | ||
) | ||
|
||
errorIndication, errorStatus, errorIndex, varBinds = await sendNotification( | ||
snmpEngine, | ||
authData, | ||
transportTarget, | ||
contextData, | ||
"inform", # NotifyType | ||
NotificationType(ObjectIdentity("SNMPv2-MIB", "coldStart")).addVarBinds( | ||
("1.3.6.1.2.1.1.1.0", "my name") | ||
), | ||
) | ||
|
||
if errorIndication: | ||
print( | ||
"Notification for {} not sent: {}".format( | ||
snmpEngine.snmpEngineID.prettyPrint(), errorIndication | ||
) | ||
) | ||
|
||
elif errorStatus: | ||
print( | ||
"Notification Receiver returned error for request %s, " | ||
"SNMP Engine: %s @%s" | ||
% (snmpEngine.snmpEngineID.prettyPrint(), errorStatus, errorIndex) | ||
) | ||
|
||
else: | ||
print( | ||
"Notification for SNMP Engine %s delivered: " | ||
% (snmpEngine.snmpEngineID.prettyPrint()) | ||
) | ||
|
||
for name, val in varBinds: | ||
print(f"{name.prettyPrint()} = {val.prettyPrint()}") | ||
|
||
transportDispatcher.runDispatcher() | ||
|
||
|
||
asyncio.run(run()) |
Oops, something went wrong.