diff options
Diffstat (limited to 'examples/hlapi/v3arch/twisted')
9 files changed, 524 insertions, 0 deletions
diff --git a/examples/hlapi/v3arch/twisted/agent/ntforg/default-v1-trap.py b/examples/hlapi/v3arch/twisted/agent/ntforg/default-v1-trap.py new file mode 100644 index 00000000..756ce58d --- /dev/null +++ b/examples/hlapi/v3arch/twisted/agent/ntforg/default-v1-trap.py @@ -0,0 +1,67 @@ +""" +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.v3arch.twisted import * + + +def success(args, hostname): + (errorStatus, errorIndex, varBinds) = args + + 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)) + + +# noinspection PyUnusedLocal +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') + ).loadMibs( + 'SNMPv2-MIB' + ).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/v3arch/twisted/agent/ntforg/multiple-notifications-at-once.py b/examples/hlapi/v3arch/twisted/agent/ntforg/multiple-notifications-at-once.py new file mode 100644 index 00000000..c234a827 --- /dev/null +++ b/examples/hlapi/v3arch/twisted/agent/ntforg/multiple-notifications-at-once.py @@ -0,0 +1,77 @@ +""" +Multiple concurrent notifications ++++++++++++++++++++++++++++++++++ + +Send multiple SNMP notifications at once using the following options: + +* SNMPv2c +* with community name 'public' +* over IPv4/UDP +* send TRAP and 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: + +| $ snmptrap -v2c -c public demo.snmplabs.com 12345 1.3.6.1.6.3.1.1.5.2 +| $ snmpinform -v2c -c public 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.v3arch.twisted import * + + +def success(args, hostname): + (errorStatus, errorIndex, varBinds) = args + + 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)) + + +# noinspection PyUnusedLocal +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') + ).loadMibs( + 'SNMPv2-MIB' + ).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, [[('demo.snmplabs.com', 'trap'), + ('demo.snmplabs.com', 'inform')]]) diff --git a/examples/hlapi/v3arch/twisted/agent/ntforg/v2c-trap-inline-callbacks.py b/examples/hlapi/v3arch/twisted/agent/ntforg/v2c-trap-inline-callbacks.py new file mode 100644 index 00000000..39b15c57 --- /dev/null +++ b/examples/hlapi/v3arch/twisted/agent/ntforg/v2c-trap-inline-callbacks.py @@ -0,0 +1,46 @@ +""" +SNMPv2c TRAP via Twisted inline callbacks ++++++++++++++++++++++++++++++++++++++++++ + +Send SNMPv2c TRAP through unified SNMPv3 message processing framework +using the following options: + +* SNMPv2c +* with community name 'public' +* over IPv4/UDP +* send TRAP notification +* with Generic Trap #1 (warmStart) and Specific Trap 0 +* include managed object information '1.3.6.1.2.1.1.1.0' = 'my system' + +Functionally similar to: + +| $ snmptrap -v2c -c public demo.snmplabs.com 12345 1.3.6.1.6.3.1.1.5.2 1.3.6.1.2.1.1.1.0 s "Hello from Twisted" + +"""# +from twisted.internet.task import react, defer +from pysnmp.hlapi.v3arch.twisted import * + + +@defer.inlineCallbacks +def sendtrap(reactor, snmpEngine, hostname): + + yield sendNotification( + snmpEngine, + CommunityData('public', mpModel=0), + UdpTransportTarget((hostname, 162)), + ContextData(), + 'trap', + NotificationType( + ObjectIdentity('1.3.6.1.6.3.1.1.5.2') + ).loadMibs( + 'SNMPv2-MIB' + ).addVarBinds( + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'), OctetString('Hello from Twisted')) + ) + ) + +# Preserve SnmpEngine instance across [potentially] multiple calls to safe on initialization +snmpEngine = SnmpEngine() + +react(sendtrap, [snmpEngine, 'demo.snmplabs.com']) + diff --git a/examples/hlapi/v3arch/twisted/manager/cmdgen/custom-timeout-and-retries.py b/examples/hlapi/v3arch/twisted/manager/cmdgen/custom-timeout-and-retries.py new file mode 100644 index 00000000..9c8c6eb9 --- /dev/null +++ b/examples/hlapi/v3arch/twisted/manager/cmdgen/custom-timeout-and-retries.py @@ -0,0 +1,53 @@ +""" +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.v3arch.twisted import * + + +def success(args, hostname): + (errorStatus, errorIndex, varBinds) = args + + 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)) + + +# noinspection PyUnusedLocal +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/v3arch/twisted/manager/cmdgen/getbulk-to-eom.py b/examples/hlapi/v3arch/twisted/manager/cmdgen/getbulk-to-eom.py new file mode 100644 index 00000000..89542793 --- /dev/null +++ b/examples/hlapi/v3arch/twisted/manager/cmdgen/getbulk-to-eom.py @@ -0,0 +1,54 @@ +""" +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.v3arch.twisted import * + + +def success(args, reactor, snmpEngine): + (errorStatus, errorIndex, varBindTable) = args + + 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/v3arch/twisted/manager/cmdgen/multiple-concurrent-queries.py b/examples/hlapi/v3arch/twisted/manager/cmdgen/multiple-concurrent-queries.py new file mode 100644 index 00000000..90561015 --- /dev/null +++ b/examples/hlapi/v3arch/twisted/manager/cmdgen/multiple-concurrent-queries.py @@ -0,0 +1,60 @@ +""" +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.v3arch.twisted import * + + +def success(args, hostname): + (errorStatus, errorIndex, varBinds) = args + + 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)) + + +# noinspection PyUnusedLocal +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/v3arch/twisted/manager/cmdgen/pull-mibs-from-multiple-agents-at-once.py b/examples/hlapi/v3arch/twisted/manager/cmdgen/pull-mibs-from-multiple-agents-at-once.py new file mode 100644 index 00000000..2e501262 --- /dev/null +++ b/examples/hlapi/v3arch/twisted/manager/cmdgen/pull-mibs-from-multiple-agents-at-once.py @@ -0,0 +1,64 @@ +""" +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.v3arch.twisted import * + + +def success(args, reactor, snmpEngine, hostname): + (errorStatus, errorIndex, varBindTable) = args + + 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/v3arch/twisted/manager/cmdgen/pull-whole-mib.py b/examples/hlapi/v3arch/twisted/manager/cmdgen/pull-whole-mib.py new file mode 100644 index 00000000..9e3f1791 --- /dev/null +++ b/examples/hlapi/v3arch/twisted/manager/cmdgen/pull-whole-mib.py @@ -0,0 +1,52 @@ +""" +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.v3arch.twisted import * + + +def success(args, reactor, snmpEngine): + (errorStatus, errorIndex, varBindTable) = args + + 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/v3arch/twisted/manager/cmdgen/v1-get.py b/examples/hlapi/v3arch/twisted/manager/cmdgen/v1-get.py new file mode 100644 index 00000000..e483354d --- /dev/null +++ b/examples/hlapi/v3arch/twisted/manager/cmdgen/v1-get.py @@ -0,0 +1,51 @@ +""" +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.v3arch.twisted import * + + +def success(args, hostname): + (errorStatus, errorIndex, varBinds) = args + + 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)) + + +# noinspection PyUnusedLocal +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']) |