summaryrefslogtreecommitdiff
path: root/examples/hlapi/twisted
diff options
context:
space:
mode:
Diffstat (limited to 'examples/hlapi/twisted')
-rw-r--r--examples/hlapi/twisted/agent/ntforg/default-v1-trap.py64
-rw-r--r--examples/hlapi/twisted/agent/ntforg/multiple-notifications-at-once.py73
-rw-r--r--examples/hlapi/twisted/manager/cmdgen/custom-timeout-and-retries.py49
-rw-r--r--examples/hlapi/twisted/manager/cmdgen/getbulk-to-eom.py52
-rw-r--r--examples/hlapi/twisted/manager/cmdgen/multiple-concurrent-async-queries.py56
-rw-r--r--examples/hlapi/twisted/manager/cmdgen/pull-mibs-from-multiple-agents-at-once.py61
-rw-r--r--examples/hlapi/twisted/manager/cmdgen/pull-whole-mib.py50
-rw-r--r--examples/hlapi/twisted/manager/cmdgen/v1-get.py47
8 files changed, 452 insertions, 0 deletions
diff --git a/examples/hlapi/twisted/agent/ntforg/default-v1-trap.py b/examples/hlapi/twisted/agent/ntforg/default-v1-trap.py
new file mode 100644
index 0000000..c17e3cf
--- /dev/null
+++ b/examples/hlapi/twisted/agent/ntforg/default-v1-trap.py
@@ -0,0 +1,64 @@
+"""
+SNMPv1 TRAP with defaults
++++++++++++++++++++++++++
+
+Send SNMPv1 TRAP through unified SNMPv3 message processing framework
+using the following options:
+
+* SNMPv1
+* with community name 'public'
+* over IPv4/UDP
+* send TRAP notification
+* with Generic Trap #1 (warmStart) and Specific Trap 0
+* with default Uptime
+* with default Agent Address
+* with Enterprise OID 1.3.6.1.4.1.20408.4.1.1.2
+* include managed object information '1.3.6.1.2.1.1.1.0' = 'my system'
+
+Functionally similar to:
+
+| $ snmptrap -v1 -c public demo.snmplabs.com \
+| 1.3.6.1.4.1.20408.4.1.1.2 \
+| 0.0.0.0 \
+| 1 \
+| 0 \
+| 0
+| '1.3.6.1.2.1.1.1.0' s 'my system'
+
+"""#
+from twisted.internet.task import react
+from pysnmp.hlapi.twisted import *
+
+def success((errorStatus, errorIndex, varBinds), hostname):
+ if errorStatus:
+ print('%s: %s at %s' % (
+ hostname,
+ errorStatus.prettyPrint(),
+ errorIndex and varBinds[int(errorIndex)-1][0] or '?'
+ )
+ )
+ else:
+ for varBind in varBinds:
+ print(' = '.join([ x.prettyPrint() for x in varBind ]))
+
+def failure(errorIndication, hostname):
+ print('%s failure: %s' % (hostname, errorIndication))
+
+def run(reactor, hostname):
+ d = sendNotification(
+ SnmpEngine(),
+ CommunityData('public', mpModel=0),
+ UdpTransportTarget((hostname, 162)),
+ ContextData(),
+ 'trap',
+ NotificationType(
+ ObjectIdentity('1.3.6.1.6.3.1.1.5.2')
+ ).addVarBinds(
+ ('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'),
+ ('1.3.6.1.2.1.1.1.0', OctetString('my system'))
+ )
+ )
+ d.addCallback(success, hostname).addErrback(failure, hostname)
+ return d
+
+react(run, ['demo.snmplabs.com'])
diff --git a/examples/hlapi/twisted/agent/ntforg/multiple-notifications-at-once.py b/examples/hlapi/twisted/agent/ntforg/multiple-notifications-at-once.py
new file mode 100644
index 0000000..e0839c3
--- /dev/null
+++ b/examples/hlapi/twisted/agent/ntforg/multiple-notifications-at-once.py
@@ -0,0 +1,73 @@
+"""
+Multiple concurrent notifications
++++++++++++++++++++++++++++++++++
+
+Send multiple SNMP notifications at once using the following options:
+
+* 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
+
+Here we tag each SNMP-COMMUNITY-MIB::snmpCommunityTable row
+with the same tag as SNMP-TARGET-MIB::snmpTargetAddrTable row
+what leads to excessive tables information.
+
+Functionally similar to:
+
+| $ snmpinform -v2c -c public demo.snmplabs.com 12345 1.3.6.1.6.3.1.1.5.2
+|
+| $ snmpinform -v3 -l authNoPriv -u usr-md5-none -A authkey1 \
+| demo.snmplabs.com \
+| 12345 \
+| 1.3.6.1.6.3.1.1.5.2
+
+"""#
+from twisted.internet.defer import DeferredList
+from twisted.internet.task import react
+from pysnmp.hlapi.twisted import *
+
+def success((errorStatus, errorIndex, varBinds), hostname):
+ if errorStatus:
+ print('%s: %s at %s' % (
+ hostname,
+ errorStatus.prettyPrint(),
+ errorIndex and varBinds[int(errorIndex)-1][0] or '?'
+ )
+ )
+ else:
+ for varBind in varBinds:
+ print(' = '.join([ x.prettyPrint() for x in varBind ]))
+
+def failure(errorIndication, hostname):
+ print('%s failure: %s' % (hostname, errorIndication))
+
+def sendone(reactor, snmpEngine, hostname, notifyType):
+ d = sendNotification(
+ snmpEngine,
+ CommunityData('public', tag=hostname),
+ UdpTransportTarget((hostname, 162), tagList=hostname),
+ ContextData(),
+ notifyType,
+ NotificationType(
+ ObjectIdentity('1.3.6.1.6.3.1.1.5.2')
+ ).addVarBinds(
+ ('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'),
+ ('1.3.6.1.2.1.1.1.0', OctetString('my system'))
+ )
+ )
+ d.addCallback(success, hostname).addErrback(failure, hostname)
+ return d
+
+def sendall(reactor, destinations):
+ snmpEngine = SnmpEngine()
+
+ return DeferredList(
+ [ sendone(reactor, snmpEngine, hostname, notifyType)
+ for hostname, notifyType in destinations ]
+ )
+
+react(sendall, [[('localhost', 'trap'), ('localhost', 'inform')]])
diff --git a/examples/hlapi/twisted/manager/cmdgen/custom-timeout-and-retries.py b/examples/hlapi/twisted/manager/cmdgen/custom-timeout-and-retries.py
new file mode 100644
index 0000000..fefcd68
--- /dev/null
+++ b/examples/hlapi/twisted/manager/cmdgen/custom-timeout-and-retries.py
@@ -0,0 +1,49 @@
+"""
+SNMPv2c
++++++++
+
+Send SNMP GET request using the following options:
+
+ * with SNMPv2c, community 'public'
+ * over IPv4/UDP with non-default timeout and no retries
+ * to an Agent at demo.snmplabs.com:161
+ * for two instances of SNMPv2-MIB::sysDescr.0 MIB object,
+ * based on Twisted I/O framework
+
+Functionally similar to:
+
+| $ snmpget -v2c -c public -r 0 -t 2 demo.snmplabs.com SNMPv2-MIB::sysDescr.0
+
+"""#
+from twisted.internet.task import react
+from pysnmp.hlapi.twisted import *
+
+def success((errorStatus, errorIndex, varBinds), hostname):
+ if errorStatus:
+ print('%s: %s at %s' % (
+ hostname,
+ errorStatus.prettyPrint(),
+ errorIndex and varBinds[int(errorIndex)-1][0] or '?'
+ )
+ )
+ else:
+ for varBind in varBinds:
+ print(' = '.join([ x.prettyPrint() for x in varBind ]))
+
+def failure(errorIndication, hostname):
+ print('%s failure: %s' % (hostname, errorIndication))
+
+def getSysDescr(reactor, hostname):
+ snmpEngine = SnmpEngine()
+
+ d = getCmd(snmpEngine,
+ CommunityData('public'),
+ UdpTransportTarget((hostname, 161), timeout=2.0, retries=0),
+ ContextData(),
+ ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)))
+
+ d.addCallback(success, hostname).addErrback(failure, hostname)
+
+ return d
+
+react(getSysDescr, ['demo.snmplabs.com'])
diff --git a/examples/hlapi/twisted/manager/cmdgen/getbulk-to-eom.py b/examples/hlapi/twisted/manager/cmdgen/getbulk-to-eom.py
new file mode 100644
index 0000000..47ede70
--- /dev/null
+++ b/examples/hlapi/twisted/manager/cmdgen/getbulk-to-eom.py
@@ -0,0 +1,52 @@
+"""
+Bulk walk MIB
++++++++++++++
+
+Send a series of SNMP GETBULK requests using the following options:
+
+* with SNMPv3, user 'usr-none-none', no authentication, no privacy
+* over IPv4/UDP
+* to an Agent at demo.snmplabs.com:161
+* for all OIDs past SNMPv2-MIB::system
+* run till end-of-mib condition is reported by Agent
+* based on Twisted I/O framework
+
+Functionally similar to:
+
+| $ snmpbulkwalk -v3 -lnoAuthNoPriv -u usr-none-none -Cn0 -Cr50 \
+| demo.snmplabs.com SNMPv2-MIB::system
+
+"""#
+from twisted.internet.task import react
+from pysnmp.hlapi.twisted import *
+
+def success((errorStatus, errorIndex, varBindTable), reactor, snmpEngine):
+ if errorStatus:
+ print('%s: %s at %s' % (
+ hostname,
+ errorStatus.prettyPrint(),
+ errorIndex and varBindTable[0][int(errorIndex)-1][0] or '?'
+ )
+ )
+ else:
+ for varBindRow in varBindTable:
+ for varBind in varBindRow:
+ print(' = '.join([ x.prettyPrint() for x in varBind ]))
+
+ if not isEndOfMib(varBindTable[-1]):
+ return getbulk(reactor, snmpEngine, *varBindTable[-1])
+
+def failure(errorIndication):
+ print(errorIndication)
+
+def getbulk(reactor, snmpEngine, varBinds):
+ d = bulkCmd(snmpEngine,
+ UsmUserData('usr-none-none'),
+ UdpTransportTarget(('demo.snmplabs.com', 161)),
+ ContextData(),
+ 0, 50,
+ varBinds)
+ d.addCallback(success, reactor, snmpEngine).addErrback(failure)
+ return d
+
+react(getbulk, [SnmpEngine(), ObjectType(ObjectIdentity('SNMPv2-MIB', 'system'))])
diff --git a/examples/hlapi/twisted/manager/cmdgen/multiple-concurrent-async-queries.py b/examples/hlapi/twisted/manager/cmdgen/multiple-concurrent-async-queries.py
new file mode 100644
index 0000000..5349e97
--- /dev/null
+++ b/examples/hlapi/twisted/manager/cmdgen/multiple-concurrent-async-queries.py
@@ -0,0 +1,56 @@
+"""
+Concurrent queries
+++++++++++++++++++
+
+Send multiple SNMP GET requests at once using the following options:
+
+* with SNMPv2c, community 'public'
+* over IPv4/UDP
+* to an Agent at demo.snmplabs.com:161
+* for two instances of SNMPv2-MIB::sysDescr.0 and SNMPv2-MIB::sysLocation.0
+ MIB object,
+* based on Twisted I/O framework
+
+Functionally similar to:
+
+| $ snmpget -v2c -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0
+| $ snmpget -v2c -c public demo.snmplabs.com SNMPv2-MIB::sysLocation.0
+
+"""#
+from twisted.internet.defer import DeferredList
+from twisted.internet.task import react
+from pysnmp.hlapi.twisted import *
+
+def success((errorStatus, errorIndex, varBinds), hostname):
+ if errorStatus:
+ print('%s: %s at %s' % (
+ hostname,
+ errorStatus.prettyPrint(),
+ errorIndex and varBinds[int(errorIndex)-1][0] or '?'
+ )
+ )
+ else:
+ for varBind in varBinds:
+ print(' = '.join([ x.prettyPrint() for x in varBind ]))
+
+def failure(errorIndication, hostname):
+ print('%s failure: %s' % (hostname, errorIndication))
+
+def getSystem(reactor, hostname):
+ snmpEngine = SnmpEngine()
+
+ def getScalar(objectType):
+ d = getCmd(snmpEngine,
+ CommunityData('public', mpModel=0),
+ UdpTransportTarget((hostname, 161)),
+ ContextData(),
+ objectType)
+ d.addCallback(success, hostname).addErrback(failure, hostname)
+ return d
+
+ return DeferredList(
+ [getScalar(ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))),
+ getScalar(ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))]
+ )
+
+react(getSystem, ['demo.snmplabs.com'])
diff --git a/examples/hlapi/twisted/manager/cmdgen/pull-mibs-from-multiple-agents-at-once.py b/examples/hlapi/twisted/manager/cmdgen/pull-mibs-from-multiple-agents-at-once.py
new file mode 100644
index 0000000..30d710b
--- /dev/null
+++ b/examples/hlapi/twisted/manager/cmdgen/pull-mibs-from-multiple-agents-at-once.py
@@ -0,0 +1,61 @@
+"""
+Walk multiple Agents at once
+++++++++++++++++++++++++++++
+
+* with SNMPv3 with user 'usr-md5-none', MD5 auth and no privacy protocols
+* over IPv4/UDP
+* to Agents at demo.snmplabs.com:161 and demo.snmplabs.com:1161
+* for multiple MIB subtrees and tables
+* for whole MIB
+* based on Twisted I/O framework
+
+Functionally similar to:
+
+| $ snmpget -v2c -c public demo.snmplabs.com:161 SNMPv2-MIB::system
+| $ snmpget -v2c -c public demo.snmplabs.comL1161 SNMPv2-MIB::system
+
+"""#
+from twisted.internet.defer import DeferredList
+from twisted.internet.task import react
+from pysnmp.hlapi.twisted import *
+
+def success((errorStatus, errorIndex, varBindTable), reactor, snmpEngine, hostname):
+ if errorStatus:
+ print('%s: %s at %s' % (
+ hostname,
+ errorStatus.prettyPrint(),
+ errorIndex and varBindTable[0][int(errorIndex)-1][0] or '?'
+ )
+ )
+ else:
+ for varBindRow in varBindTable:
+ for varBind in varBindRow:
+ print(' = '.join([ x.prettyPrint() for x in varBind ]))
+
+ if not isEndOfMib(varBindTable[-1]):
+ return getbulk(reactor, snmpEngine, hostname, *varBindTable[-1])
+
+def failure(errorIndication):
+ print(errorIndication)
+
+def getbulk(reactor, snmpEngine, hostname, varBinds):
+ d = bulkCmd(snmpEngine,
+ UsmUserData('usr-md5-none', 'authkey1'),
+ UdpTransportTarget(hostname),
+ ContextData(),
+ 0, 25,
+ varBinds)
+ d.addCallback(success, reactor, snmpEngine, hostname).addErrback(failure)
+ return d
+
+def getall(reactor, hostnames):
+ snmpEngine = SnmpEngine()
+
+ return DeferredList(
+ [ getbulk(reactor, snmpEngine, hostname,
+ ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')))
+ for hostname in hostnames ]
+ )
+
+react(getall, [(('demo.snmplabs.com', 161), ('demo.snmplabs.com', 1161))])
+
diff --git a/examples/hlapi/twisted/manager/cmdgen/pull-whole-mib.py b/examples/hlapi/twisted/manager/cmdgen/pull-whole-mib.py
new file mode 100644
index 0000000..6df1668
--- /dev/null
+++ b/examples/hlapi/twisted/manager/cmdgen/pull-whole-mib.py
@@ -0,0 +1,50 @@
+"""
+Walk whole MIB
+++++++++++++++
+
+Send a series of SNMP GETNEXT requests using the following options:
+
+* with SNMPv3, user 'usr-md5-none', MD5 authentication, no privacy
+* over IPv4/UDP
+* to an Agent at demo.snmplabs.com:161
+* for all OIDs in IF-MIB
+* based on Twisted I/O framework
+
+Functionally similar to:
+
+| $ snmpwalk -v3 -lauthPriv -u usr-md5-none -A authkey1 -X privkey1 \
+| demo.snmplabs.com IF-MIB::
+
+"""#
+from twisted.internet.task import react
+from pysnmp.hlapi.twisted import *
+
+def success((errorStatus, errorIndex, varBindTable), reactor, snmpEngine):
+ if errorStatus:
+ print('%s: %s at %s' % (
+ hostname,
+ errorStatus.prettyPrint(),
+ errorIndex and varBindTable[0][int(errorIndex)-1][0] or '?'
+ )
+ )
+ else:
+ for varBindRow in varBindTable:
+ for varBind in varBindRow:
+ print(' = '.join([ x.prettyPrint() for x in varBind ]))
+
+ if not isEndOfMib(varBindTable[-1]):
+ return getnext(reactor, snmpEngine, *varBindTable[-1])
+
+def failure(errorIndication):
+ print(errorIndication)
+
+def getnext(reactor, snmpEngine, varBinds):
+ d = nextCmd(snmpEngine,
+ UsmUserData('usr-md5-none', 'authkey1'),
+ UdpTransportTarget(('demo.snmplabs.com', 161)),
+ ContextData(),
+ varBinds)
+ d.addCallback(success, reactor, snmpEngine).addErrback(failure)
+ return d
+
+react(getnext, [SnmpEngine(), ObjectType(ObjectIdentity('SNMPv2-MIB', 'system'))])
diff --git a/examples/hlapi/twisted/manager/cmdgen/v1-get.py b/examples/hlapi/twisted/manager/cmdgen/v1-get.py
new file mode 100644
index 0000000..02b3690
--- /dev/null
+++ b/examples/hlapi/twisted/manager/cmdgen/v1-get.py
@@ -0,0 +1,47 @@
+"""
+SNMPv1
+++++++
+
+Send SNMP GET request using the following options:
+
+* with SNMPv1, community 'public'
+* over IPv4/UDP
+* to an Agent at demo.snmplabs.com:161
+* for two instances of SNMPv2-MIB::sysDescr.0 MIB object,
+* based on Twisted I/O framework
+
+Functionally similar to:
+
+| $ snmpget -v1 -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0
+
+"""#
+from twisted.internet.task import react
+from pysnmp.hlapi.twisted import *
+
+def success((errorStatus, errorIndex, varBinds), hostname):
+ if errorStatus:
+ print('%s: %s at %s' % (
+ hostname,
+ errorStatus.prettyPrint(),
+ errorIndex and varBinds[int(errorIndex)-1][0] or '?'
+ )
+ )
+ else:
+ for varBind in varBinds:
+ print(' = '.join([ x.prettyPrint() for x in varBind ]))
+
+def failure(errorIndication, hostname):
+ print('%s failure: %s' % (hostname, errorIndication))
+
+def getSysDescr(reactor, hostname):
+ d = getCmd(SnmpEngine(),
+ CommunityData('public', mpModel=0),
+ UdpTransportTarget((hostname, 161)),
+ ContextData(),
+ ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)))
+
+ d.addCallback(success, hostname).addErrback(failure, hostname)
+
+ return d
+
+react(getSysDescr, ['demo.snmplabs.com'])