From ac0b956d006a4b7b32780e852740b56ecd826c7e Mon Sep 17 00:00:00 2001 From: Ilya Etingof Date: Sun, 12 Aug 2018 17:22:58 +0200 Subject: Add `hlapi.v1arch` API (#186) * Add `hlapi.v1arch` API Introduce new sub-package `pysnmp.hlapi.v1arch` which wraps otherwise very detailed packet-level SNMP messaging into a handful of convenience functions. As a side effect, the `pysnmp.hlapi.*` sub-packages moved under `pysnmp.hlapi.v3arch` though `pysnmp.hlapi` still exposes `pysnmp.hlappi.v3arch.*` symbols to retain some degree of backward compatibility. The signature of the hlapi `.sendNotification()` call has changed to accept `*varBinds` instead of a sequence of `varBinds`. The rationale is to unify this method call with similar methods of CommandGenerator. * Add v1arch docs and reshuffle hlapi docs --- .../hlapi/asyncio/agent/ntforg/default-v1-trap.py | 50 -------- .../agent/ntforg/multiple-notifications-at-once.py | 64 ---------- .../hlapi/asyncio/manager/cmdgen/getbulk-to-eom.py | 63 ---------- ...ltiple-concurrent-queries-over-ipv4-and-ipv6.py | 54 --------- .../manager/cmdgen/multiple-sequential-queries.py | 58 --------- examples/hlapi/asyncio/manager/cmdgen/v1-get.py | 48 -------- .../hlapi/asyncore/agent/ntforg/default-v1-trap.py | 41 ------- .../agent/ntforg/multiple-informs-at-once.py | 64 ---------- .../agent/ntforg/multiple-traps-at-once.py | 52 --------- .../running-multiple-snmp-engines-at-once.py | 92 --------------- ...ltiple-concurrent-queries-over-ipv4-and-ipv6.py | 80 ------------- ...m-multiple-agents-at-once-over-ipv4-and-ipv6.py | 73 ------------ .../asyncore/manager/cmdgen/pull-whole-mib.py | 49 -------- ...ery-multiple-snmp-engines-over-ipv4-and-ipv6.py | 95 --------------- examples/hlapi/asyncore/manager/cmdgen/v2c-get.py | 44 ------- .../sync/agent/ntforg/custom-contextengineid.py | 41 ------- .../sync/agent/ntforg/custom-contextname.py | 41 ------- .../asyncore/sync/agent/ntforg/custom-v1-trap.py | 45 ------- .../asyncore/sync/agent/ntforg/default-v1-trap.py | 42 ------- .../send-notification-with-additional-varbinds.py | 43 ------- .../ntforg/v2c-trap-with-notification-objects.py | 41 ------- .../hlapi/asyncore/sync/agent/ntforg/v3-inform.py | 43 ------- .../hlapi/asyncore/sync/agent/ntforg/v3-trap.py | 41 ------- .../manager/cmdgen/coerce-set-value-to-mib-spec.py | 35 ------ .../manager/cmdgen/custom-asn1-mib-search-path.py | 36 ------ .../custom-contextengineid-and-contextname.py | 38 ------ .../sync/manager/cmdgen/custom-contextengineid.py | 37 ------ .../cmdgen/custom-pysnmp-mibs-search-path.py | 44 ------- .../manager/cmdgen/custom-timeout-and-retries.py | 50 -------- .../sync/manager/cmdgen/custom-v3-security-name.py | 35 ------ .../manager/cmdgen/fetch-variables-over-ipv6.py | 35 ------ .../manager/cmdgen/get-table-object-by-index.py | 36 ------ .../cmdgen/get-table-object-by-multiple-indices.py | 39 ------- ...k-fetch-scalar-and-table-variables-over-ipv6.py | 42 ------- .../cmdgen/getbulk-limit-number-of-packets.py | 41 ------- .../cmdgen/getbulk-limit-number-of-variables.py | 43 ------- .../cmdgen/getnext-limit-number-of-variables.py | 42 ------- .../manager/cmdgen/getnext-multiple-oids-to-eom.py | 40 ------- .../sync/manager/cmdgen/multiple-get-calls.py | 43 ------- .../sync/manager/cmdgen/preload-pysnmp-mibs.py | 38 ------ .../asyncore/sync/manager/cmdgen/pull-whole-mib.py | 37 ------ .../sync/manager/cmdgen/pull-whole-snmp-table.py | 44 ------- ...nts-from-multuple-threads-over-ipv4-and-ipv6.py | 124 -------------------- .../manager/cmdgen/set-multiple-scalar-values.py | 43 ------- .../sync/manager/cmdgen/specific-v3-engine-id.py | 83 ------------- .../asyncore/sync/manager/cmdgen/usm-md5-des.py | 34 ------ .../asyncore/sync/manager/cmdgen/usm-md5-none.py | 34 ------ .../asyncore/sync/manager/cmdgen/usm-none-none.py | 33 ------ .../asyncore/sync/manager/cmdgen/usm-sha-aes128.py | 55 --------- .../hlapi/asyncore/sync/manager/cmdgen/v1-get.py | 34 ------ .../hlapi/asyncore/sync/manager/cmdgen/v2c-get.py | 35 ------ .../asyncore/sync/manager/cmdgen/v2c-getbulk.py | 42 ------- .../sync/manager/cmdgen/waive-mib-lookup.py | 43 ------- .../hlapi/trollius/agent/ntforg/default-v1-trap.py | 62 ---------- .../trollius/manager/cmdgen/getbulk-to-eom.py | 61 ---------- examples/hlapi/trollius/manager/cmdgen/v1-get.py | 48 -------- .../hlapi/twisted/agent/ntforg/default-v1-trap.py | 65 ----------- .../agent/ntforg/multiple-notifications-at-once.py | 75 ------------ .../agent/ntforg/v2c-trap-inline-callbacks.py | 44 ------- .../manager/cmdgen/custom-timeout-and-retries.py | 53 --------- .../hlapi/twisted/manager/cmdgen/getbulk-to-eom.py | 54 --------- .../manager/cmdgen/multiple-concurrent-queries.py | 60 ---------- .../pull-mibs-from-multiple-agents-at-once.py | 64 ---------- .../hlapi/twisted/manager/cmdgen/pull-whole-mib.py | 52 --------- examples/hlapi/twisted/manager/cmdgen/v1-get.py | 51 -------- .../asyncore/agent/ntforg/generic-v1-trap.py | 52 +++++++++ .../asyncore/agent/ntforg/generic-v2c-trap.py | 44 +++++++ .../v1arch/asyncore/agent/ntforg/v2c-inform.py | 51 ++++++++ .../ntforg/v2c-trap-with-notification-objects.py | 53 +++++++++ ...ltiple-concurrent-queries-over-ipv4-and-ipv6.py | 63 ++++++++++ ...m-multiple-agents-at-once-over-ipv4-and-ipv6.py | 63 ++++++++++ .../asyncore/manager/cmdgen/pull-whole-mib.py | 44 +++++++ .../v1arch/asyncore/manager/cmdgen/v1-getnext.py | 48 ++++++++ .../v1arch/asyncore/manager/cmdgen/v2c-get.py | 44 +++++++ .../asyncore/sync/agent/ntforg/generic-v1-trap.py | 46 ++++++++ .../asyncore/sync/agent/ntforg/generic-v2c-trap.py | 38 ++++++ .../asyncore/sync/agent/ntforg/v2c-inform.py | 45 +++++++ .../ntforg/v2c-trap-with-notification-objects.py | 39 +++++++ .../manager/cmdgen/coerce-set-value-to-mib-spec.py | 38 ++++++ .../manager/cmdgen/custom-asn1-mib-search-path.py | 40 +++++++ .../cmdgen/custom-pysnmp-mibs-search-path.py | 49 ++++++++ .../manager/cmdgen/custom-timeout-and-retries.py | 51 ++++++++ .../sync/manager/cmdgen/enable-mib-lookup.py | 43 +++++++ .../manager/cmdgen/fetch-variables-over-ipv6.py | 36 ++++++ .../manager/cmdgen/get-table-object-by-index.py | 38 ++++++ .../cmdgen/get-table-object-by-multiple-indices.py | 40 +++++++ ...k-fetch-scalar-and-table-variables-over-ipv6.py | 45 +++++++ .../cmdgen/getbulk-limit-number-of-packets.py | 44 +++++++ .../sync/manager/cmdgen/multiple-get-calls.py | 48 ++++++++ .../sync/manager/cmdgen/preload-pysnmp-mibs.py | 41 +++++++ .../asyncore/sync/manager/cmdgen/pull-whole-mib.py | 39 +++++++ .../sync/manager/cmdgen/pull-whole-snmp-table.py | 47 ++++++++ ...nts-from-multuple-threads-over-ipv4-and-ipv6.py | 130 +++++++++++++++++++++ .../manager/cmdgen/set-multiple-scalar-values.py | 46 ++++++++ .../v1arch/asyncore/sync/manager/cmdgen/v1-get.py | 36 ++++++ .../v1arch/asyncore/sync/manager/cmdgen/v2c-get.py | 35 ++++++ .../v3arch/asyncio/agent/ntforg/default-v1-trap.py | 52 +++++++++ .../agent/ntforg/multiple-notifications-at-once.py | 66 +++++++++++ .../asyncio/manager/cmdgen/getbulk-to-eom.py | 63 ++++++++++ ...ltiple-concurrent-queries-over-ipv4-and-ipv6.py | 54 +++++++++ .../manager/cmdgen/multiple-sequential-queries.py | 58 +++++++++ .../hlapi/v3arch/asyncio/manager/cmdgen/v1-get.py | 48 ++++++++ .../asyncore/agent/ntforg/default-v1-trap.py | 43 +++++++ .../agent/ntforg/multiple-informs-at-once.py | 64 ++++++++++ .../agent/ntforg/multiple-traps-at-once.py | 52 +++++++++ .../running-multiple-snmp-engines-at-once.py | 92 +++++++++++++++ ...ltiple-concurrent-queries-over-ipv4-and-ipv6.py | 80 +++++++++++++ ...m-multiple-agents-at-once-over-ipv4-and-ipv6.py | 73 ++++++++++++ .../asyncore/manager/cmdgen/pull-whole-mib.py | 49 ++++++++ ...ery-multiple-snmp-engines-over-ipv4-and-ipv6.py | 95 +++++++++++++++ .../v3arch/asyncore/manager/cmdgen/v2c-get.py | 44 +++++++ .../sync/agent/ntforg/custom-contextengineid.py | 44 +++++++ .../sync/agent/ntforg/custom-contextname.py | 41 +++++++ .../asyncore/sync/agent/ntforg/custom-v1-trap.py | 47 ++++++++ .../asyncore/sync/agent/ntforg/default-v1-trap.py | 44 +++++++ .../send-notification-with-additional-varbinds.py | 43 +++++++ .../ntforg/v2c-trap-with-notification-objects.py | 41 +++++++ .../v3arch/asyncore/sync/agent/ntforg/v3-inform.py | 45 +++++++ .../v3arch/asyncore/sync/agent/ntforg/v3-trap.py | 41 +++++++ .../manager/cmdgen/coerce-set-value-to-mib-spec.py | 35 ++++++ .../manager/cmdgen/custom-asn1-mib-search-path.py | 36 ++++++ .../custom-contextengineid-and-contextname.py | 38 ++++++ .../sync/manager/cmdgen/custom-contextengineid.py | 37 ++++++ .../cmdgen/custom-pysnmp-mibs-search-path.py | 44 +++++++ .../manager/cmdgen/custom-timeout-and-retries.py | 50 ++++++++ .../sync/manager/cmdgen/custom-v3-security-name.py | 35 ++++++ .../manager/cmdgen/fetch-variables-over-ipv6.py | 35 ++++++ .../manager/cmdgen/get-table-object-by-index.py | 36 ++++++ .../cmdgen/get-table-object-by-multiple-indices.py | 39 +++++++ ...k-fetch-scalar-and-table-variables-over-ipv6.py | 42 +++++++ .../cmdgen/getbulk-limit-number-of-packets.py | 41 +++++++ .../cmdgen/getbulk-limit-number-of-variables.py | 43 +++++++ .../cmdgen/getnext-limit-number-of-variables.py | 42 +++++++ .../manager/cmdgen/getnext-multiple-oids-to-eom.py | 40 +++++++ .../sync/manager/cmdgen/multiple-get-calls.py | 44 +++++++ .../sync/manager/cmdgen/preload-pysnmp-mibs.py | 38 ++++++ .../asyncore/sync/manager/cmdgen/pull-whole-mib.py | 37 ++++++ .../sync/manager/cmdgen/pull-whole-snmp-table.py | 44 +++++++ ...nts-from-multuple-threads-over-ipv4-and-ipv6.py | 124 ++++++++++++++++++++ .../manager/cmdgen/set-multiple-scalar-values.py | 43 +++++++ .../sync/manager/cmdgen/specific-v3-engine-id.py | 83 +++++++++++++ .../asyncore/sync/manager/cmdgen/usm-md5-des.py | 34 ++++++ .../asyncore/sync/manager/cmdgen/usm-md5-none.py | 34 ++++++ .../asyncore/sync/manager/cmdgen/usm-none-none.py | 33 ++++++ .../asyncore/sync/manager/cmdgen/usm-sha-aes128.py | 55 +++++++++ .../v3arch/asyncore/sync/manager/cmdgen/v1-get.py | 34 ++++++ .../v3arch/asyncore/sync/manager/cmdgen/v2c-get.py | 35 ++++++ .../asyncore/sync/manager/cmdgen/v2c-getbulk.py | 42 +++++++ .../sync/manager/cmdgen/waive-mib-lookup.py | 42 +++++++ .../trollius/agent/ntforg/default-v1-trap.py | 64 ++++++++++ .../trollius/manager/cmdgen/getbulk-to-eom.py | 61 ++++++++++ .../hlapi/v3arch/trollius/manager/cmdgen/v1-get.py | 48 ++++++++ .../v3arch/twisted/agent/ntforg/default-v1-trap.py | 67 +++++++++++ .../agent/ntforg/multiple-notifications-at-once.py | 77 ++++++++++++ .../agent/ntforg/v2c-trap-inline-callbacks.py | 46 ++++++++ .../manager/cmdgen/custom-timeout-and-retries.py | 53 +++++++++ .../twisted/manager/cmdgen/getbulk-to-eom.py | 54 +++++++++ .../manager/cmdgen/multiple-concurrent-queries.py | 60 ++++++++++ .../pull-mibs-from-multiple-agents-at-once.py | 64 ++++++++++ .../twisted/manager/cmdgen/pull-whole-mib.py | 52 +++++++++ .../hlapi/v3arch/twisted/manager/cmdgen/v1-get.py | 51 ++++++++ .../agent/ntforg/send-inform-over-ipv4-and-ipv6.py | 1 - 162 files changed, 4767 insertions(+), 3269 deletions(-) delete mode 100644 examples/hlapi/asyncio/agent/ntforg/default-v1-trap.py delete mode 100644 examples/hlapi/asyncio/agent/ntforg/multiple-notifications-at-once.py delete mode 100644 examples/hlapi/asyncio/manager/cmdgen/getbulk-to-eom.py delete mode 100644 examples/hlapi/asyncio/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py delete mode 100644 examples/hlapi/asyncio/manager/cmdgen/multiple-sequential-queries.py delete mode 100644 examples/hlapi/asyncio/manager/cmdgen/v1-get.py delete mode 100644 examples/hlapi/asyncore/agent/ntforg/default-v1-trap.py delete mode 100644 examples/hlapi/asyncore/agent/ntforg/multiple-informs-at-once.py delete mode 100644 examples/hlapi/asyncore/agent/ntforg/multiple-traps-at-once.py delete mode 100644 examples/hlapi/asyncore/agent/ntforg/running-multiple-snmp-engines-at-once.py delete mode 100644 examples/hlapi/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py delete mode 100644 examples/hlapi/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py delete mode 100644 examples/hlapi/asyncore/manager/cmdgen/pull-whole-mib.py delete mode 100644 examples/hlapi/asyncore/manager/cmdgen/query-multiple-snmp-engines-over-ipv4-and-ipv6.py delete mode 100644 examples/hlapi/asyncore/manager/cmdgen/v2c-get.py delete mode 100644 examples/hlapi/asyncore/sync/agent/ntforg/custom-contextengineid.py delete mode 100644 examples/hlapi/asyncore/sync/agent/ntforg/custom-contextname.py delete mode 100644 examples/hlapi/asyncore/sync/agent/ntforg/custom-v1-trap.py delete mode 100644 examples/hlapi/asyncore/sync/agent/ntforg/default-v1-trap.py delete mode 100644 examples/hlapi/asyncore/sync/agent/ntforg/send-notification-with-additional-varbinds.py delete mode 100644 examples/hlapi/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py delete mode 100644 examples/hlapi/asyncore/sync/agent/ntforg/v3-inform.py delete mode 100644 examples/hlapi/asyncore/sync/agent/ntforg/v3-trap.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/custom-contextengineid-and-contextname.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/custom-contextengineid.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/custom-v3-security-name.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/get-table-object-by-index.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-variables.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/getnext-limit-number-of-variables.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/getnext-multiple-oids-to-eom.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/multiple-get-calls.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/pull-whole-mib.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/specific-v3-engine-id.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/usm-md5-des.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/usm-md5-none.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/usm-none-none.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/usm-sha-aes128.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/v1-get.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/v2c-get.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/v2c-getbulk.py delete mode 100644 examples/hlapi/asyncore/sync/manager/cmdgen/waive-mib-lookup.py delete mode 100644 examples/hlapi/trollius/agent/ntforg/default-v1-trap.py delete mode 100644 examples/hlapi/trollius/manager/cmdgen/getbulk-to-eom.py delete mode 100644 examples/hlapi/trollius/manager/cmdgen/v1-get.py delete mode 100644 examples/hlapi/twisted/agent/ntforg/default-v1-trap.py delete mode 100644 examples/hlapi/twisted/agent/ntforg/multiple-notifications-at-once.py delete mode 100644 examples/hlapi/twisted/agent/ntforg/v2c-trap-inline-callbacks.py delete mode 100644 examples/hlapi/twisted/manager/cmdgen/custom-timeout-and-retries.py delete mode 100644 examples/hlapi/twisted/manager/cmdgen/getbulk-to-eom.py delete mode 100644 examples/hlapi/twisted/manager/cmdgen/multiple-concurrent-queries.py delete mode 100644 examples/hlapi/twisted/manager/cmdgen/pull-mibs-from-multiple-agents-at-once.py delete mode 100644 examples/hlapi/twisted/manager/cmdgen/pull-whole-mib.py delete mode 100644 examples/hlapi/twisted/manager/cmdgen/v1-get.py create mode 100644 examples/hlapi/v1arch/asyncore/agent/ntforg/generic-v1-trap.py create mode 100644 examples/hlapi/v1arch/asyncore/agent/ntforg/generic-v2c-trap.py create mode 100644 examples/hlapi/v1arch/asyncore/agent/ntforg/v2c-inform.py create mode 100644 examples/hlapi/v1arch/asyncore/agent/ntforg/v2c-trap-with-notification-objects.py create mode 100644 examples/hlapi/v1arch/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py create mode 100644 examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py create mode 100644 examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-whole-mib.py create mode 100644 examples/hlapi/v1arch/asyncore/manager/cmdgen/v1-getnext.py create mode 100644 examples/hlapi/v1arch/asyncore/manager/cmdgen/v2c-get.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v1-trap.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v2c-trap.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-inform.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/enable-mib-lookup.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v1-get.py create mode 100644 examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v2c-get.py create mode 100644 examples/hlapi/v3arch/asyncio/agent/ntforg/default-v1-trap.py create mode 100644 examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-notifications-at-once.py create mode 100644 examples/hlapi/v3arch/asyncio/manager/cmdgen/getbulk-to-eom.py create mode 100644 examples/hlapi/v3arch/asyncio/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py create mode 100644 examples/hlapi/v3arch/asyncio/manager/cmdgen/multiple-sequential-queries.py create mode 100644 examples/hlapi/v3arch/asyncio/manager/cmdgen/v1-get.py create mode 100644 examples/hlapi/v3arch/asyncore/agent/ntforg/default-v1-trap.py create mode 100644 examples/hlapi/v3arch/asyncore/agent/ntforg/multiple-informs-at-once.py create mode 100644 examples/hlapi/v3arch/asyncore/agent/ntforg/multiple-traps-at-once.py create mode 100644 examples/hlapi/v3arch/asyncore/agent/ntforg/running-multiple-snmp-engines-at-once.py create mode 100644 examples/hlapi/v3arch/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py create mode 100644 examples/hlapi/v3arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py create mode 100644 examples/hlapi/v3arch/asyncore/manager/cmdgen/pull-whole-mib.py create mode 100644 examples/hlapi/v3arch/asyncore/manager/cmdgen/query-multiple-snmp-engines-over-ipv4-and-ipv6.py create mode 100644 examples/hlapi/v3arch/asyncore/manager/cmdgen/v2c-get.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-contextengineid.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-contextname.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-v1-trap.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/agent/ntforg/default-v1-trap.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/agent/ntforg/send-notification-with-additional-varbinds.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v3-inform.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v3-trap.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-contextengineid-and-contextname.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-contextengineid.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-v3-security-name.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-variables.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getnext-limit-number-of-variables.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getnext-multiple-oids-to-eom.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/specific-v3-engine-id.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-md5-des.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-md5-none.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-none-none.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-sha-aes128.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v1-get.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v2c-get.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v2c-getbulk.py create mode 100644 examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/waive-mib-lookup.py create mode 100644 examples/hlapi/v3arch/trollius/agent/ntforg/default-v1-trap.py create mode 100644 examples/hlapi/v3arch/trollius/manager/cmdgen/getbulk-to-eom.py create mode 100644 examples/hlapi/v3arch/trollius/manager/cmdgen/v1-get.py create mode 100644 examples/hlapi/v3arch/twisted/agent/ntforg/default-v1-trap.py create mode 100644 examples/hlapi/v3arch/twisted/agent/ntforg/multiple-notifications-at-once.py create mode 100644 examples/hlapi/v3arch/twisted/agent/ntforg/v2c-trap-inline-callbacks.py create mode 100644 examples/hlapi/v3arch/twisted/manager/cmdgen/custom-timeout-and-retries.py create mode 100644 examples/hlapi/v3arch/twisted/manager/cmdgen/getbulk-to-eom.py create mode 100644 examples/hlapi/v3arch/twisted/manager/cmdgen/multiple-concurrent-queries.py create mode 100644 examples/hlapi/v3arch/twisted/manager/cmdgen/pull-mibs-from-multiple-agents-at-once.py create mode 100644 examples/hlapi/v3arch/twisted/manager/cmdgen/pull-whole-mib.py create mode 100644 examples/hlapi/v3arch/twisted/manager/cmdgen/v1-get.py (limited to 'examples') diff --git a/examples/hlapi/asyncio/agent/ntforg/default-v1-trap.py b/examples/hlapi/asyncio/agent/ntforg/default-v1-trap.py deleted file mode 100644 index 92d3e70e..00000000 --- a/examples/hlapi/asyncio/agent/ntforg/default-v1-trap.py +++ /dev/null @@ -1,50 +0,0 @@ -""" -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" - -"""# -import asyncio -from pysnmp.hlapi.asyncio import * - - -@asyncio.coroutine -def run(): - snmpEngine = SnmpEngine() - errorIndication, errorStatus, errorIndex, varBinds = yield from sendNotification( - snmpEngine, - CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 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')) - ) - ) - - if errorIndication: - print(errorIndication) - - snmpEngine.transportDispatcher.closeDispatcher() - - -asyncio.get_event_loop().run_until_complete(run()) diff --git a/examples/hlapi/asyncio/agent/ntforg/multiple-notifications-at-once.py b/examples/hlapi/asyncio/agent/ntforg/multiple-notifications-at-once.py deleted file mode 100644 index 74cedeea..00000000 --- a/examples/hlapi/asyncio/agent/ntforg/multiple-notifications-at-once.py +++ /dev/null @@ -1,64 +0,0 @@ -""" -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 - -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 -| $ snmptrap -v2c -c public demo.snmplabs.com 12345 1.3.6.1.6.3.1.1.5.2 - -"""# -import asyncio -from pysnmp.hlapi.asyncio import * - - -@asyncio.coroutine -def sendone(snmpEngine, hostname, notifyType): - (errorIndication, - errorStatus, - errorIndex, - varBinds) = yield from 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')) - ) - ) - - if errorIndication: - print(errorIndication) - elif errorStatus: - print('%s: at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) - - -snmpEngine = SnmpEngine() - -loop = asyncio.get_event_loop() -loop.run_until_complete( - asyncio.wait([sendone(snmpEngine, 'demo.snmplabs.com', 'trap'), - sendone(snmpEngine, 'demo.snmplabs.com', 'inform')]) -) diff --git a/examples/hlapi/asyncio/manager/cmdgen/getbulk-to-eom.py b/examples/hlapi/asyncio/manager/cmdgen/getbulk-to-eom.py deleted file mode 100644 index e453e6b5..00000000 --- a/examples/hlapi/asyncio/manager/cmdgen/getbulk-to-eom.py +++ /dev/null @@ -1,63 +0,0 @@ -""" -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 asyncio I/O framework - -Functionally similar to: - -| $ snmpbulkwalk -v3 -lnoAuthNoPriv -u usr-none-none -Cn0 -Cr50 \ -| demo.snmplabs.com SNMPv2-MIB::system - -"""# -import asyncio -from pysnmp.hlapi.asyncio import * - - -@asyncio.coroutine -def run(varBinds): - snmpEngine = SnmpEngine() - while True: - (errorIndication, - errorStatus, - errorIndex, - varBindTable) = yield from bulkCmd( - snmpEngine, - UsmUserData('usr-none-none'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - 0, 50, - *varBinds) - - if errorIndication: - print(errorIndication) - break - elif errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?' - ) - ) - else: - for varBindRow in varBindTable: - for varBind in varBindRow: - print(' = '.join([x.prettyPrint() for x in varBind])) - - varBinds = varBindTable[-1] - if isEndOfMib(varBinds): - break - - snmpEngine.transportDispatcher.closeDispatcher() - - -loop = asyncio.get_event_loop() -loop.run_until_complete( - run([ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr'))]) -) diff --git a/examples/hlapi/asyncio/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py b/examples/hlapi/asyncio/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py deleted file mode 100644 index ad9c441b..00000000 --- a/examples/hlapi/asyncio/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py +++ /dev/null @@ -1,54 +0,0 @@ -""" -Concurrent queries -++++++++++++++++++ - -Send multiple SNMP GET requests at once using the following options: - -* with SNMPv2c, community 'public' -* over IPv4/UDP -* to multiple Agents at demo.snmplabs.com -* for instance of SNMPv2-MIB::sysDescr.0 MIB object -* based on asyncio I/O framework - -Functionally similar to: - -| $ snmpget -v2c -c public demo.snmplabs.com:1161 SNMPv2-MIB::sysDescr.0 -| $ snmpget -v2c -c public demo.snmplabs.com:2161 SNMPv2-MIB::sysDescr.0 -| $ snmpget -v2c -c public demo.snmplabs.com:3161 SNMPv2-MIB::sysDescr.0 - -"""# -import asyncio -from pysnmp.hlapi.asyncio import * - - -@asyncio.coroutine -def getone(snmpEngine, hostname): - errorIndication, errorStatus, errorIndex, varBinds = yield from getCmd( - snmpEngine, - CommunityData('public'), - UdpTransportTarget(hostname), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)) - ) - - if errorIndication: - print(errorIndication) - elif errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?' - ) - ) - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) - - -snmpEngine = SnmpEngine() - -loop = asyncio.get_event_loop() -loop.run_until_complete( - asyncio.wait([getone(snmpEngine, ('demo.snmplabs.com', 1161)), - getone(snmpEngine, ('demo.snmplabs.com', 2161)), - getone(snmpEngine, ('demo.snmplabs.com', 3161))]) -) diff --git a/examples/hlapi/asyncio/manager/cmdgen/multiple-sequential-queries.py b/examples/hlapi/asyncio/manager/cmdgen/multiple-sequential-queries.py deleted file mode 100644 index 4a43bc49..00000000 --- a/examples/hlapi/asyncio/manager/cmdgen/multiple-sequential-queries.py +++ /dev/null @@ -1,58 +0,0 @@ -""" -Sequential queries -++++++++++++++++++ - -Send multiple SNMP GET requests one by one using the following options: - -* with SNMPv2c, community 'public' -* over IPv4/UDP -* to multiple Agents at demo.snmplabs.com -* for instance of SNMPv2-MIB::sysDescr.0 MIB object -* based on asyncio I/O framework - -Functionally similar to: - -| $ snmpget -v2c -c public demo.snmplabs.com:1161 SNMPv2-MIB::sysDescr.0 -| $ snmpget -v2c -c public demo.snmplabs.com:2161 SNMPv2-MIB::sysDescr.0 -| $ snmpget -v2c -c public demo.snmplabs.com:3161 SNMPv2-MIB::sysDescr.0 - -"""# -import asyncio -from pysnmp.hlapi.asyncio import * - - -@asyncio.coroutine -def getone(snmpEngine, hostname): - errorIndication, errorStatus, errorIndex, varBinds = yield from getCmd( - snmpEngine, - CommunityData('public'), - UdpTransportTarget(hostname), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)) - ) - - if errorIndication: - print(errorIndication) - elif errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?' - ) - ) - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) - - -@asyncio.coroutine -def getall(snmpEngine, hostnames): - for hostname in hostnames: - yield from getone(snmpEngine, hostname) - - -snmpEngine = SnmpEngine() - -loop = asyncio.get_event_loop() -loop.run_until_complete(getall(snmpEngine, [('demo.snmplabs.com', 1161), - ('demo.snmplabs.com', 2161), - ('demo.snmplabs.com', 3161)])) diff --git a/examples/hlapi/asyncio/manager/cmdgen/v1-get.py b/examples/hlapi/asyncio/manager/cmdgen/v1-get.py deleted file mode 100644 index 6147706d..00000000 --- a/examples/hlapi/asyncio/manager/cmdgen/v1-get.py +++ /dev/null @@ -1,48 +0,0 @@ -""" -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 an instance of SNMPv2-MIB::sysDescr.0 MIB object - * Based on asyncio I/O framework - -Functionally similar to: - -| $ snmpget -v1 -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 - -"""# -import asyncio -from pysnmp.hlapi.asyncio import * - - -@asyncio.coroutine -def run(): - snmpEngine = SnmpEngine() - errorIndication, errorStatus, errorIndex, varBinds = yield from getCmd( - snmpEngine, - CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)) - ) - - if errorIndication: - print(errorIndication) - elif errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?' - ) - ) - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) - - snmpEngine.transportDispatcher.closeDispatcher() - - -asyncio.get_event_loop().run_until_complete(run()) diff --git a/examples/hlapi/asyncore/agent/ntforg/default-v1-trap.py b/examples/hlapi/asyncore/agent/ntforg/default-v1-trap.py deleted file mode 100644 index 3b806d6d..00000000 --- a/examples/hlapi/asyncore/agent/ntforg/default-v1-trap.py +++ /dev/null @@ -1,41 +0,0 @@ -""" -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 pysnmp.hlapi.asyncore import * - -snmpEngine = SnmpEngine() - -sendNotification( - snmpEngine, - CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 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')) - ) -) - -snmpEngine.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/asyncore/agent/ntforg/multiple-informs-at-once.py b/examples/hlapi/asyncore/agent/ntforg/multiple-informs-at-once.py deleted file mode 100644 index 58c5bfb8..00000000 --- a/examples/hlapi/asyncore/agent/ntforg/multiple-informs-at-once.py +++ /dev/null @@ -1,64 +0,0 @@ -""" -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 - -| $ snmpinform -v2c -c public demo.snmplabs.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.snmplabs.com 123 1.3.6.1.6.3.1.1.5.1 - -"""# -from pysnmp.hlapi.asyncore import * - -# List of targets in the followin format: -# ( ( authData, transportTarget ), ... ) -targets = ( - # 1-st target (SNMPv2c over IPv4/UDP) - (CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData()), - # 2-nd target (SNMPv3 over IPv4/UDP) - (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData()), -) - - -# noinspection PyUnusedLocal -def cbFun(snmpEngine, sendRequestHandle, errorIndication, - errorStatus, errorIndex, varBinds, cbCtx): - if errorIndication: - print('Notification %s not sent: %s' % (sendRequestHandle, errorIndication)) - elif errorStatus: - print('Notification Receiver returned error for %s: %s @%s' % - (sendRequestHandle, errorStatus, errorIndex)) - else: - print('Notification %s delivered:' % sendRequestHandle) - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -snmpEngine = SnmpEngine() - -for authData, transportTarget, contextData in targets: - sendPduHandle = sendNotification( - snmpEngine, - authData, - transportTarget, - contextData, - 'inform', # NotifyType - NotificationType( - ObjectIdentity('SNMPv2-MIB', 'coldStart') - ).addVarBinds(('1.3.6.1.2.1.1.1.0', 'my name')), - cbFun=cbFun - ) - -snmpEngine.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/asyncore/agent/ntforg/multiple-traps-at-once.py b/examples/hlapi/asyncore/agent/ntforg/multiple-traps-at-once.py deleted file mode 100644 index 7aa63f68..00000000 --- a/examples/hlapi/asyncore/agent/ntforg/multiple-traps-at-once.py +++ /dev/null @@ -1,52 +0,0 @@ -""" -Multiple concurrent queries -+++++++++++++++++++++++++++ - -Send a bunch of different SNMP Notifications to different peers all at once, -wait for responses asynchronously: - -* SNMPv1 and SNMPv2c -* 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 - -| $ snmptrap -v1 -c public demo.snmplabs.com 1.3.6.1.4.1.20408.4.1.1.2 demo.snmplabs.com 6 432 12345 1.3.6.1.2.1.1.1.0 s "my system" -| $ snmptrap -v2c -c public demo.snmplabs.com 123 1.3.6.1.6.3.1.1.5.1 - -"""# -from pysnmp.hlapi.asyncore import * - -# List of targets in the followin format: -# ( ( authData, transportTarget ), ... ) -targets = ( - # 1-st target (SNMPv1 over IPv4/UDP) - (CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData()), - # 2-nd target (SNMPv2c over IPv4/UDP) - (CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData()), -) - -snmpEngine = SnmpEngine() - -for authData, transportTarget, contextData in targets: - sendNotification( - snmpEngine, - authData, - transportTarget, - contextData, - 'trap', # NotifyType - NotificationType( - ObjectIdentity('SNMPv2-MIB', 'coldStart') - ).addVarBinds( - (ObjectIdentifier('1.3.6.1.2.1.1.1.0'), - OctetString('my name')) - ) - ) - -snmpEngine.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/asyncore/agent/ntforg/running-multiple-snmp-engines-at-once.py b/examples/hlapi/asyncore/agent/ntforg/running-multiple-snmp-engines-at-once.py deleted file mode 100644 index ba65d60a..00000000 --- a/examples/hlapi/asyncore/agent/ntforg/running-multiple-snmp-engines-at-once.py +++ /dev/null @@ -1,92 +0,0 @@ -""" -Multiple SNMP Engines -+++++++++++++++++++++ - -Send SNMP notifications in behalf of multiple independend 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 IMFORM 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 -criterias are also possible. - -| $ snmpinform -v2c -c public demo.snmplabs.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.snmplabs.com 123 1.3.6.1.6.3.1.1.5.1 - -"""# -from pysnmp.hlapi.asyncore import * -from pysnmp.carrier.asyncore.dispatch import AsyncoreDispatcher - -# List of targets in the following format: -# ( ( authData, transportTarget ), ... ) -targets = ( - # 1-st target (SNMPv2c over IPv4/UDP) - (CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 1162)), - ContextData()), - # 2-nd target (SNMPv3 over IPv4/UDP) - (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData()), -) - - -# noinspection PyUnusedLocal -def cbFun(snmpEngine, sendRequestHandle, errorIndication, - errorStatus, errorIndex, varBinds, cbCtx): - snmpEngine = cbCtx - if errorIndication: - print('Notification %s for %s not sent: %s' % (sendRequestHandle, snmpEngine.snmpEngineID.prettyPrint(), errorIndication)) - elif errorStatus: - print('Notification Receiver returned error for request %s, SNMP Engine %s: %s @%s' % (sendRequestHandle, snmpEngine.snmpEngineID.prettyPrint(), errorStatus, errorIndex)) - else: - print('Notification %s for SNMP Engine %s delivered:' % (sendRequestHandle, snmpEngine.snmpEngineID.prettyPrint())) - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Instantiate the single transport dispatcher object -transportDispatcher = AsyncoreDispatcher() - -# 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: - snmpEngine = (transportTarget.getTransportInfo()[1][1] % 3 and - snmpEngineA or snmpEngineB) - sendPduHandle = sendNotification( - snmpEngine, - authData, - transportTarget, - contextData, - 'inform', # NotifyType - NotificationType( - ObjectIdentity('SNMPv2-MIB', 'coldStart') - ).addVarBinds(('1.3.6.1.2.1.1.1.0', 'my name')), - cbFun=cbFun, cbCtx=snmpEngine - ) - -transportDispatcher.runDispatcher() diff --git a/examples/hlapi/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py b/examples/hlapi/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py deleted file mode 100644 index 2b4b0eb3..00000000 --- a/examples/hlapi/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py +++ /dev/null @@ -1,80 +0,0 @@ -""" -Multiple concurrent queries -+++++++++++++++++++++++++++ - -Send a bunch of different SNMP GET requests to different peers all at once, -wait for responses asynchronously: - -* with SNMPv1, community 'public' and - with SNMPv2c, community 'public' and - with SNMPv3, user 'usr-md5-des', MD5 auth and DES privacy -* over IPv4/UDP and - over IPv6/UDP -* to an Agent at demo.snmplabs.com:161 and - to an Agent at [::1]:161 -* for instances of SNMPv2-MIB::sysDescr.0 and - SNMPv2-MIB::sysLocation.0 MIB objects - -"""# -from pysnmp.hlapi.asyncore import * - -# List of targets in the followin format: -# ( ( authData, transportTarget, varNames ), ... ) -targets = ( - # 1-st target (SNMPv1 over IPv4/UDP) - (CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 161)), - (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), - # 2-nd target (SNMPv2c over IPv4/UDP) - (CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), - # 3-nd target (SNMPv2c over IPv4/UDP) - same community and - # different transport address. - (CommunityData('public'), - UdpTransportTarget(('localhost', 161)), - (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysContact', 0)), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)))), - # 4-nd target (SNMPv3 over IPv4/UDP) - (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), - # 5-th target (SNMPv3 over IPv6/UDP) - (UsmUserData('usr-md5-none', 'authkey1'), - Udp6TransportTarget(('::1', 161)), - (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), - # N-th target - # ... -) - - -# Wait for responses or errors -# noinspection PyUnusedLocal,PyUnusedLocal -def cbFun(snmpEngine, sendRequestHandle, errorIndication, - errorStatus, errorIndex, varBinds, cbCtx): - authData, transportTarget = cbCtx - print('%s via %s' % (authData, transportTarget)) - if errorIndication: - print(errorIndication) - return True - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) - return True - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) - - -snmpEngine = SnmpEngine() - -# Submit GET requests -for authData, transportTarget, varNames in targets: - getCmd(snmpEngine, authData, transportTarget, ContextData(), *varNames, - **dict(cbFun=cbFun, cbCtx=(authData, transportTarget))) - -snmpEngine.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py b/examples/hlapi/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py deleted file mode 100644 index d87d55c5..00000000 --- a/examples/hlapi/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py +++ /dev/null @@ -1,73 +0,0 @@ -""" -Walk multiple Agents at once -++++++++++++++++++++++++++++ - -Iterate over MIBs of multiple SNMP Agents asynchronously using the -following options: - -* with SNMPv1, community 'public' and - with SNMPv2c, community 'public' and - with SNMPv3, user 'usr-md5-des', MD5 auth and DES privacy -* over IPv4/UDP and - over IPv6/UDP -* to an Agent at demo.snmplabs.com:161 and - to an Agent at [::1]:161 -* pull variables till EOM - -"""# -from pysnmp.hlapi.asyncore import * - -# List of targets in the followin format: -# ( ( authData, transportTarget, varNames ), ... ) -targets = ( - # 1-st target (SNMPv1 over IPv4/UDP) - (CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 161)), - (ObjectType(ObjectIdentity('1.3.6.1.2.1')), - ObjectType(ObjectIdentity('1.3.6.1.3.1')))), - # 2-nd target (SNMPv2c over IPv4/UDP) - (CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - (ObjectType(ObjectIdentity('1.3.6.1.4.1')),)), - # 3-nd target (SNMPv3 over IPv4/UDP) - (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - (ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')),)), - # 4-th target (SNMPv3 over IPv6/UDP) - (UsmUserData('usr-md5-none', 'authkey1'), - Udp6TransportTarget(('::1', 161)), - (ObjectType(ObjectIdentity('IF-MIB', 'ifTable')),)) - # N-th target - # ... -) - - -# Wait for responses or errors, submit GETNEXT requests for further OIDs -# noinspection PyUnusedLocal,PyUnusedLocal -def cbFun(snmpEngine, sendRequestHandle, errorIndication, - errorStatus, errorIndex, varBindTable, cbCtx): - (authData, transportTarget) = cbCtx - print('%s via %s' % (authData, transportTarget)) - if errorIndication: - print(errorIndication) - return - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBindTable[-1][int(errorIndex) - 1][0] or '?')) - return - else: - for varBindRow in varBindTable: - for varBind in varBindRow: - print(' = '.join([x.prettyPrint() for x in varBind])) - - return True # continue table retrieval - - -snmpEngine = SnmpEngine() - -# Submit initial GETNEXT requests and wait for responses -for authData, transportTarget, varBinds in targets: - nextCmd(snmpEngine, authData, transportTarget, ContextData(), - *varBinds, **dict(cbFun=cbFun, cbCtx=(authData, transportTarget))) - -snmpEngine.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/asyncore/manager/cmdgen/pull-whole-mib.py b/examples/hlapi/asyncore/manager/cmdgen/pull-whole-mib.py deleted file mode 100644 index 8d9ff19e..00000000 --- a/examples/hlapi/asyncore/manager/cmdgen/pull-whole-mib.py +++ /dev/null @@ -1,49 +0,0 @@ -""" -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 - -Functionally similar to: - -| $ snmpwalk -v3 -lauthNoPriv -u usr-md5-none -A authkey1 -X privkey1 \ -| demo.snmplabs.com IF-MIB:: - -"""# -from pysnmp.hlapi.asyncore import * - - -# noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal -def cbFun(snmpEngine, sendRequestHandle, errorIndication, - errorStatus, errorIndex, varBindTable, cbCtx): - if errorIndication: - print(errorIndication) - return - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBindTable[-1][int(errorIndex) - 1][0] or '?')) - return - else: - for varBindRow in varBindTable: - for varBind in varBindRow: - print(' = '.join([x.prettyPrint() for x in varBind])) - - return True # request lower layers to do GETNEXT and call us back - - -snmpEngine = SnmpEngine() - -nextCmd(snmpEngine, - UsmUserData('usr-md5-none', 'authkey1'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')), - ObjectType(ObjectIdentity('IF-MIB', 'ifTable')), - cbFun=cbFun) - -snmpEngine.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/asyncore/manager/cmdgen/query-multiple-snmp-engines-over-ipv4-and-ipv6.py b/examples/hlapi/asyncore/manager/cmdgen/query-multiple-snmp-engines-over-ipv4-and-ipv6.py deleted file mode 100644 index 4a732041..00000000 --- a/examples/hlapi/asyncore/manager/cmdgen/query-multiple-snmp-engines-over-ipv4-and-ipv6.py +++ /dev/null @@ -1,95 +0,0 @@ -""" -Multiple SNMP engines -+++++++++++++++++++++ - -Send multiple SNMP GET requests to multiple peers using multiple -independend SNMP engines. Deal with peers asynchronously. SNMP options -are: - -* with SNMPv1, community 'public' and - with SNMPv2c, community 'public' and - with SNMPv3, user 'usr-md5-des', MD5 auth and DES privacy -* over IPv4/UDP and - over IPv6/UDP -* to an Agent at demo.snmplabs.com:161 and - to an Agent at [::1]:161 -* for instances of SNMPv2-MIB::sysDescr.0 and - SNMPv2-MIB::sysLocation.0 MIB objects - -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 AsyncCommandGenerator with each of -SNMP Engines to comunicate GET command request 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 -criterias are also possible. - -"""# -from pysnmp.hlapi.asyncore import * -from pysnmp.carrier.asyncore.dispatch import AsyncoreDispatcher - -# List of targets in the following format: -# ( ( authData, transportTarget, varNames ), ... ) -targets = ( - # 1-st target (SNMPv1 over IPv4/UDP) - (CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 161)), - (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), - # 2-nd target (SNMPv2c over IPv4/UDP) - (CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 1161)), - (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), - # 3-nd target (SNMPv3 over IPv4/UDP) - (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - UdpTransportTarget(('demo.snmplabs.com', 2161)), - (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))) - # N-th target - # ... -) - - -# Wait for responses or errors -# noinspection PyUnusedLocal,PyUnusedLocal -def cbFun(snmpEngine, sendRequestHandle, errorIndication, - errorStatus, errorIndex, varBinds, cbCtx): - (snmpEngine, authData, transportTarget) = cbCtx - print('snmpEngine %s: %s via %s' % (snmpEngine.snmpEngineID.prettyPrint(), authData, transportTarget)) - if errorIndication: - print(errorIndication) - return True - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) - return True - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) - - -# Instantiate the single transport dispatcher object -transportDispatcher = AsyncoreDispatcher() - -# 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, varBinds in targets: - snmpEngine = transportTarget.getTransportInfo()[1][1] % 3 and \ - snmpEngineA or snmpEngineB - getCmd(snmpEngine, authData, transportTarget, ContextData(), *varBinds, - **dict(cbFun=cbFun, cbCtx=(snmpEngine, authData, transportTarget))) - -transportDispatcher.runDispatcher() diff --git a/examples/hlapi/asyncore/manager/cmdgen/v2c-get.py b/examples/hlapi/asyncore/manager/cmdgen/v2c-get.py deleted file mode 100644 index d752b540..00000000 --- a/examples/hlapi/asyncore/manager/cmdgen/v2c-get.py +++ /dev/null @@ -1,44 +0,0 @@ -""" -SNMPv2c -+++++++ - -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, - -Functionally similar to: - -| $ snmpget -v2c -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 - -"""# -from pysnmp.hlapi.asyncore import * - - -# noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal -def cbFun(snmpEngine, sendRequestHandle, errorIndication, - errorStatus, errorIndex, varBinds, cbCtx): - if errorIndication: - print(errorIndication) - return - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBindTable[-1][int(errorIndex) - 1][0] or '?')) - return - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) - - -snmpEngine = SnmpEngine() - -getCmd(snmpEngine, - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), - cbFun=cbFun) - -snmpEngine.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/asyncore/sync/agent/ntforg/custom-contextengineid.py b/examples/hlapi/asyncore/sync/agent/ntforg/custom-contextengineid.py deleted file mode 100644 index 6c4e7adc..00000000 --- a/examples/hlapi/asyncore/sync/agent/ntforg/custom-contextengineid.py +++ /dev/null @@ -1,41 +0,0 @@ -""" -INFORM with custom ContextEngineId -++++++++++++++++++++++++++++++++++ - -Send SNMP notification using the following options: - -* SNMPv3 -* with user 'usr-md5-none', MD5 auth, no priv -* send INFORM notification -* in behalf of contextEngineId 0x8000000004030201, contextName '' -* over IPv4/UDP -* with TRAP ID 'warmStart' specified as a string OID - -Sending SNMPv3 Notification in behalf of non-default ContextEngineId -requires having a collection of Managed Objects registered under -the ContextEngineId being used. - -Functionally similar to: - -| $ snmpinform -v3 -l authNoPriv -u usr-md5-none -A authkey1 -E 0x8000000004030201 demo.snmplabs.com 12345 1.3.6.1.6.3.1.1.5.2 - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - sendNotification(SnmpEngine(), - UsmUserData('usr-md5-none', 'authkey1'), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData(OctetString(hexValue='8000000004030201')), - 'inform', - NotificationType(ObjectIdentity('1.3.6.1.6.3.1.1.5.2'))) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/agent/ntforg/custom-contextname.py b/examples/hlapi/asyncore/sync/agent/ntforg/custom-contextname.py deleted file mode 100644 index 729c9b83..00000000 --- a/examples/hlapi/asyncore/sync/agent/ntforg/custom-contextname.py +++ /dev/null @@ -1,41 +0,0 @@ -""" -INFORM with custom ContextName -++++++++++++++++++++++++++++++ - -Send SNMP notification using the following options: - -* SNMPv3 -* with user 'usr-md5-none', MD5 auth, no priv -* send INFORM notification -* in behalf of contextEngineId = SnmpEngineId, contextName 'my-context' -* over IPv4/UDP -* with TRAP ID 'warmStart' specified as a string OID - -Sending SNMPv3 Notification in behalf of non-default ContextName -requires having a collection of Managed Objects registered under -the ContextName being used. - -Functionally similar to: - -| $ snmpinform -v3 -l authNoPriv -u usr-md5-none -A authkey1 -n my-context demo.snmplabs.com 12345 0.3.6.1.6.3.1.1.5.2 - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - sendNotification(SnmpEngine(), - UsmUserData('usr-md5-none', 'authkey1'), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData(contextName='my-context'), - 'inform', - NotificationType(ObjectIdentity('1.3.6.1.6.3.1.1.5.2'))) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/agent/ntforg/custom-v1-trap.py b/examples/hlapi/asyncore/sync/agent/ntforg/custom-v1-trap.py deleted file mode 100644 index 8693f13f..00000000 --- a/examples/hlapi/asyncore/sync/agent/ntforg/custom-v1-trap.py +++ /dev/null @@ -1,45 +0,0 @@ -""" -Custom SNMPv1 TRAP -++++++++++++++++++ - -Send SNMPv1 TRAP through unified SNMPv3 message processing framework. - -Original v1 TRAP fields are mapped into dedicated variable-bindings, -(see `RFC2576 `_) for details. - -* SNMPv1 -* with community name 'public' -* over IPv4/UDP -* send TRAP notification -* with Generic Trap #6 (enterpriseSpecific) and Specific Trap 432 -* overriding Uptime value with 12345 -* overriding Agent Address with '127.0.0.1' -* overriding Enterprise OID with 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 127.0.0.1 6 432 12345 1.3.6.1.2.1.1.1.0 s "my system" - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - sendNotification( - SnmpEngine(), - CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData(), - 'trap', - NotificationType( - ObjectIdentity('1.3.6.1.4.1.20408.4.1.1.2.0.432'), - ).addVarBinds( - ('1.3.6.1.2.1.1.3.0', 12345), - ('1.3.6.1.6.3.18.1.3.0', '127.0.0.1'), - ('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')) - ) - ) -) -if errorIndication: - print(errorIndication) diff --git a/examples/hlapi/asyncore/sync/agent/ntforg/default-v1-trap.py b/examples/hlapi/asyncore/sync/agent/ntforg/default-v1-trap.py deleted file mode 100644 index 18cc2c77..00000000 --- a/examples/hlapi/asyncore/sync/agent/ntforg/default-v1-trap.py +++ /dev/null @@ -1,42 +0,0 @@ -""" -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 pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - sendNotification( - SnmpEngine(), - CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 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')) - ) - ) -) - -if errorIndication: - print(errorIndication) diff --git a/examples/hlapi/asyncore/sync/agent/ntforg/send-notification-with-additional-varbinds.py b/examples/hlapi/asyncore/sync/agent/ntforg/send-notification-with-additional-varbinds.py deleted file mode 100644 index 46f3c5c1..00000000 --- a/examples/hlapi/asyncore/sync/agent/ntforg/send-notification-with-additional-varbinds.py +++ /dev/null @@ -1,43 +0,0 @@ -""" -Sending additional var-binds -++++++++++++++++++++++++++++ - -Send SNMP notification using the following options: - -* SNMPv2c -* with community name 'public' -* over IPv4/UDP -* send INFORM notification -* with TRAP ID 'coldStart' specified as a MIB symbol -* include managed object information specified as a MIB symbol - -Functionally similar to: - -| $ snmpinform -v2c -c public demo.snmplabs.com 12345 1.3.6.1.6.3.1.1.5.1 1.3.6.1.2.1.1.1.0 s "my system" - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - sendNotification( - SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData(), - 'inform', - NotificationType( - ObjectIdentity('SNMPv2-MIB', 'coldStart') - ).addVarBinds( - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0), 'my system') - ) - ) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py b/examples/hlapi/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py deleted file mode 100644 index 5681d28c..00000000 --- a/examples/hlapi/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py +++ /dev/null @@ -1,41 +0,0 @@ -""" -SNMPv2c TRAP via NOTIFICATION-TYPE -++++++++++++++++++++++++++++++++++ - -Initialize TRAP message contents from variables specified -in *NOTIFICATION-TYPE* SMI macro. - -* SNMPv2c -* with community name 'public' -* over IPv4/UDP -* send TRAP notification -* with TRAP ID 'linkUp' specified as a MIB symbol -* include values for managed objects implicitly added to notification - (via NOTIFICATION-TYPE->OBJECTS) - -Functionally similar to: - -| $ snmptrap -v2c -c public demo.snmplabs.com 0 1.3.6.1.6.3.1.1.5.1 1.3.6.1.2.1.2.2.1.1.123 i 123 1.3.6.1.2.1.2.2.1.7.123 i 1 1.3.6.1.2.1.2.2.1.8.123 i 1 - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - sendNotification( - SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData(), - 'trap', - NotificationType( - ObjectIdentity('IF-MIB', 'linkUp'), - instanceIndex=(123,), - objects={('IF-MIB', 'ifIndex'): 123, - ('IF-MIB', 'ifAdminStatus'): 'up', - ('IF-MIB', 'ifOperStatus'): 'up'} - ) - ) -) - -if errorIndication: - print(errorIndication) diff --git a/examples/hlapi/asyncore/sync/agent/ntforg/v3-inform.py b/examples/hlapi/asyncore/sync/agent/ntforg/v3-inform.py deleted file mode 100644 index 46153978..00000000 --- a/examples/hlapi/asyncore/sync/agent/ntforg/v3-inform.py +++ /dev/null @@ -1,43 +0,0 @@ -""" -INFORM, auth: MD5 privacy: DES -++++++++++++++++++++++++++++++ - -Send SNMP INFORM notification using the following options: - -* SNMPv3 -* with user 'usr-md5-des', auth: MD5, priv DES -* over IPv4/UDP -* send INFORM notification -* with TRAP ID 'warmStart' specified as a string OID -* include managed object information 1.3.6.1.2.1.1.5.0 = 'system name' - -Functionally similar to: - -| $ snmpinform -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 demo.snmplabs.com 12345 1.3.6.1.4.1.20408.4.1.1.2 1.3.6.1.2.1.1.1.0 s "my system" - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - sendNotification( - SnmpEngine(), - UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData(), - 'inform', - NotificationType( - ObjectIdentity('1.3.6.1.6.3.1.1.5.2') - ).addVarBinds( - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.5.0'), 'system name') - ) - ) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/agent/ntforg/v3-trap.py b/examples/hlapi/asyncore/sync/agent/ntforg/v3-trap.py deleted file mode 100644 index 836e8d50..00000000 --- a/examples/hlapi/asyncore/sync/agent/ntforg/v3-trap.py +++ /dev/null @@ -1,41 +0,0 @@ -""" -SNMPv3 TRAP: auth SHA, privacy: AES128 -++++++++++++++++++++++++++++++++++++++ - -Send SNMP notification using the following options: - -* SNMPv3 -* with authoritative snmpEngineId = 0x8000000001020304 - (USM must be configured at the Receiver accordingly) -* with user 'usr-sha-aes128', auth: SHA, priv: AES128 -* over IPv4/UDP -* send TRAP notification -* with TRAP ID 'authenticationFailure' specified as a MIB symbol -* do not include any additional managed object information - -SNMPv3 TRAPs requires pre-sharing the Notification Originator's -value of SnmpEngineId with Notification Receiver. To facilitate that -we will use static (e.g. not autogenerated) version of snmpEngineId. - -Functionally similar to: - -| $ snmptrap -v3 -e 8000000001020304 -l authPriv -u usr-sha-aes -A authkey1 -X privkey1 -a SHA -x AES demo.snmplabs.com 12345 1.3.6.1.4.1.20408.4.1.1.2 1.3.6.1.2.1.1.1.0 s "my system" - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - sendNotification( - SnmpEngine(OctetString(hexValue='8000000001020304')), - UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', - authProtocol=usmHMACSHAAuthProtocol, - privProtocol=usmAesCfb128Protocol), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData(), - 'trap', - NotificationType(ObjectIdentity('SNMPv2-MIB', 'authenticationFailure')) - ) -) - -if errorIndication: - print(errorIndication) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py b/examples/hlapi/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py deleted file mode 100644 index b6fb879e..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py +++ /dev/null @@ -1,35 +0,0 @@ -""" -Coerce value to SET to MIB spec -+++++++++++++++++++++++++++++++ - -Send SNMP SET request using the following options: - -* with SNMPv2c, community 'public' -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* setting SNMPv2-MIB::sysName.0 to new value (type taken from MIB) - -Functionally similar to: - -| $ snmpset -v2c -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 = "new system name" - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - setCmd(SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysORDescr', 1), - 'new system name')) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py deleted file mode 100644 index f25fafe4..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py +++ /dev/null @@ -1,36 +0,0 @@ -""" -Custom ASN.1 MIB path -+++++++++++++++++++++ - -Send SNMP GET request using the following options: - -* with SNMPv2c, community 'public' -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* for IF-MIB::ifInOctets.1 MIB object -* pass non-default ASN.1 MIB source to MIB compiler - -Functionally similar to: - -| $ snmpget -v2c -c public -M /usr/share/snmp demo.snmplabs.com IF-MIB::ifInOctets.1 - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1).addAsn1MibSource('file:///usr/share/snmp', - 'http://mibs.snmplabs.com/asn1/@mib@'))) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-contextengineid-and-contextname.py b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-contextengineid-and-contextname.py deleted file mode 100644 index 497af602..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-contextengineid-and-contextname.py +++ /dev/null @@ -1,38 +0,0 @@ -""" -Custom ContextEngineId and ContextName -++++++++++++++++++++++++++++++++++++++ - -Send SNMP SET request using the following options: - -* with SNMPv3 with user 'usr-md5-none', MD5 auth and no privacy protocols -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* addressing particular set of Managed Objects at remote SNMP Engine by: - * contextEngineId 0x80004fb805636c6f75644dab22cc and - * contextName 'a172334d7d97871b72241397f713fa12' -* setting SNMPv2-MIB::sysName.0 to new value (type taken from MIB) - -Functionally similar to: - -| $ snmpset -v3 -u usr-md5-des -l authPriv -A authkey1 -X privkey1 -E 80004fb805636c6f75644dab22cc -n a172334d7d97871b72241397f713fa12 demo.snmplabs.com SNMPv2-MIB::sysORDescr.1 = "new system name" - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - setCmd(SnmpEngine(), - UsmUserData('usr-md5-none', 'authkey1'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(contextEngineId=OctetString(hexValue='80004fb805636c6f75644dab22cc'), - contextName='da761cfc8c94d3aceef4f60f049105ba'), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysORDescr', 1), 'new system name')) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-contextengineid.py b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-contextengineid.py deleted file mode 100644 index 6435a5d5..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-contextengineid.py +++ /dev/null @@ -1,37 +0,0 @@ -""" -Custom ContextEngineId -++++++++++++++++++++++ - -Send SNMP GET request using the following options: - -* with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols -* use remote SNMP Engine ID 0x80004fb805636c6f75644dab22cc (USM - autodiscovery will run) -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* setting SNMPv2-MIB::sysName.0 to new value (type coerced from MIB) - -Functionally similar to: - -| $ snmpset -v3 -u usr-md5-des -l authPriv -A authkey1 -X privkey1 -E 80004fb805636c6f75644dab22cc demo.snmplabs.com SNMPv2-MIB::sysORDescr.1 = "new system name" - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - setCmd(SnmpEngine(), - UsmUserData('usr-md5-des', 'authkey1', 'privkey1', - securityEngineId=OctetString(hexValue='80004fb805636c6f75644dab22cc')), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0), 'new system name')) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py deleted file mode 100644 index 26b8954a..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py +++ /dev/null @@ -1,44 +0,0 @@ -""" -Custom PySNMP MIBs location -+++++++++++++++++++++++++++ - -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 within TCP-MIB::tcpConnTable column -* TCP-MIB Python module will be searched by a user-specified filesystem - path (/opt/mib/pysnmp) and in Python package (python_packaged_mibs) - which should be in sys.path - -Functionally similar to: - -| $ snmpbulkwalk -v3 -lnoAuthNoPriv -u usr-none-none -Cn0 -Cr50 demo.snmplabs.com TCP-MIB::tcpConnTable - -"""# -from pysnmp.hlapi import * - -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in bulkCmd(SnmpEngine(), - UsmUserData('usr-none-none'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - 0, 50, - ObjectType( - ObjectIdentity('TCP-MIB', 'tcpConnTable').addMibSource('/opt/mibs/pysnmp').addMibSource('python_packaged_mibs') - ), - lexicographicMode=False): - - if errorIndication: - print(errorIndication) - break - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) - break - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py deleted file mode 100644 index d70a322f..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py +++ /dev/null @@ -1,50 +0,0 @@ -""" -Custom request timeout -++++++++++++++++++++++ - -Send SNMP GET request using the following options: - -* with SNMPv2c, community 'public' -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* for an OID in string form -* use custom timeout and request retries values - -Transport timing settings (maximum number of request retries and -individual request timeout in seconds) can be set on a per-target basis -as explained by the code that follows. - -Keep in mind that while timeout value can be specified in fractions of a -second, default pysnmp timer resolution is quite low (about 0.5 sec) -so there's no much point in using timeouts which is not a multiple of 0.5 -Internal timer can be programmatically adjusted to finer resolution if needed. - -If retries value is set to 0, pysnmp will issue a single request. Even -if no response arrives, there will be no retry. Likewise, retries=1 -means one initial request plus one retry. - -Functionally similar to: - -| $ snmpget -v1 -c public -t 2 -r 0 demo.snmplabs.com 1.3.6.1.2.1.1.1.0 - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - CommunityData('public'), - UdpTransportTarget( - ('demo.snmplabs.com', 161), timeout=2.0, retries=0 - ), - ContextData(), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'))) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-v3-security-name.py b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-v3-security-name.py deleted file mode 100644 index 8e541f7b..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-v3-security-name.py +++ /dev/null @@ -1,35 +0,0 @@ -""" -Custom SecurityName -+++++++++++++++++++ - -Send SNMP GET request using the following options: - -* with SNMPv3, user 'usr-md5-none', securityName 'myuser' - MD5 authentication, no privacy -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* for an OID in text form - -The securityName parameter can be thought as an alias to userName and -allows you to address a USM Table row just as userName does. However -securityName can be made human-readable, also it is not an index in -usmUserTable, thus duplicate securityName parameters are possible. -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - UsmUserData('usr-md5-none', 'authkey1', securityName='myuser'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'))) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py b/examples/hlapi/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py deleted file mode 100644 index 9bc76446..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py +++ /dev/null @@ -1,35 +0,0 @@ -""" -GET over IPv6 -+++++++++++++ - -Send SNMP GET request using the following options: - -* with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols -* over IPv6/UDP -* to an Agent at [::1]:161 -* for three OIDs in string form - -Functionally similar to: - -| $ snmpget -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 udp6:[::1]:161 1.3.6.1.2.1.1.1.0 1.3.6.1.2.1.1.2.0 1.3.6.1.2.1.1.3.0 -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - Udp6TransportTarget(('::1', 161)), - ContextData(), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0')), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.2.0')), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.3.0'))) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/get-table-object-by-index.py b/examples/hlapi/asyncore/sync/manager/cmdgen/get-table-object-by-index.py deleted file mode 100644 index ed5d8351..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/get-table-object-by-index.py +++ /dev/null @@ -1,36 +0,0 @@ -""" -GET table row -+++++++++++++ - -Send SNMP GET request 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 IF-MIB::ifInOctets.1 and IF-MIB::ifOutOctets.1 MIB object -* perform response OIDs and values resolution at MIB - -Functionally similar to: - -| $ snmpget -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 demo.snmplabs.com IF-MIB::ifInOctets.1 IF-MIB::ifOutOctets.1 - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - UsmUserData('usr-none-none'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1)), - ObjectType(ObjectIdentity('IF-MIB', 'ifOutOctets', 1))) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py b/examples/hlapi/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py deleted file mode 100644 index 672aea93..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py +++ /dev/null @@ -1,39 +0,0 @@ -""" -Fetch table row by composite index -++++++++++++++++++++++++++++++++++ - -Send SNMP GET request using the following options: - -* with SNMPv3, user 'usr-sha-aes128', SHA auth, AES128 privacy -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* for TCP-MIB::tcpConnLocalAddress."0.0.0.0".22."0.0.0.0".0 MIB object - -Functionally similar to: - -| $ snmpget -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 -a SHA -x AES demo.snmplabs.com TCP-MIB::tcpConnLocalAddress."0.0.0.0".22."0.0.0.0".0 - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', - authProtocol=usmHMACSHAAuthProtocol, - privProtocol=usmAesCfb128Protocol), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('TCP-MIB', - 'tcpConnLocalAddress', - '0.0.0.0', 22, - '0.0.0.0', 0))) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py b/examples/hlapi/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py deleted file mode 100644 index 499c9b02..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py +++ /dev/null @@ -1,42 +0,0 @@ -""" -Fetch scalar and table variables -++++++++++++++++++++++++++++++++ - -Send a series of SNMP GETBULK requests using the following options: - -* with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols -* over IPv6/UDP -* to an Agent at [::1]:161 -* with values non-repeaters = 1, max-repetitions = 25 -* for IP-MIB::ipAdEntAddr and all columns of the IF-MIB::ifEntry table -* stop when response OIDs leave the scopes of the table - -Functionally similar to: - -| $ snmpbulkwalk -v3 -lauthPriv -u usr-md5-des -A authkey1 -X privkey1 -Cn1, -Cr25 demo.snmplabs.com IP-MIB::ipAdEntAddr IP-MIB::ipAddrEntry - -"""# -from pysnmp.hlapi import * - -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in bulkCmd(SnmpEngine(), - UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - Udp6TransportTarget(('::1', 161)), - ContextData(), - 1, 25, - ObjectType(ObjectIdentity('IP-MIB', 'ipAdEntAddr')), - ObjectType(ObjectIdentity('IP-MIB', 'ipAddrEntry')), - lexicographicMode=False): - - if errorIndication: - print(errorIndication) - break - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) - break - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py b/examples/hlapi/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py deleted file mode 100644 index dd94650c..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py +++ /dev/null @@ -1,41 +0,0 @@ -""" -Walk Agent, limit number of packets -+++++++++++++++++++++++++++++++++++ - -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 OR - maxCalls == 10 request-response interactions occur - -Functionally similar to: - -| $ snmpbulkwalk -v3 -lnoAuthNoPriv -u usr-none-none -Cn0 -Cr50 demo.snmplabs.com SNMPv2-MIB::system - -"""# -from pysnmp.hlapi import * - -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in bulkCmd(SnmpEngine(), - UsmUserData('usr-none-none'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - 0, 50, - ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')), - maxCalls=10): - - if errorIndication: - print(errorIndication) - break - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) - break - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-variables.py b/examples/hlapi/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-variables.py deleted file mode 100644 index fa39086b..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-variables.py +++ /dev/null @@ -1,43 +0,0 @@ -""" -Walk MIB, limit number of response rows -+++++++++++++++++++++++++++++++++++++++ - -Send a series of SNMP GETBULK requests using the following options: - -* with SNMPv2c, community 'public' -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* with values non-repeaters = 0, max-repetitions = 25 -* for two OIDs in string form -* stop when response OIDs leave the scopes of initial OIDs OR - number of response rows reach fixed value (20) - -Functionally similar to: - -| $ snmpbulkwalk -v2c -c public demo.snmplabs.com -Cn0 -Cr25 1.3.6.1.2.1.2.2 1.3.6.1.2.1.2.3 - -"""# -from pysnmp.hlapi import * - -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in bulkCmd(SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - 0, 25, - ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2')), - ObjectType(ObjectIdentity('1.3.6.1.2.1.2.3')), - lexicographicMode=False, maxRows=20): - - if errorIndication: - print(errorIndication) - break - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex)-1][0] or '?')) - break - else: - for varBind in varBinds: - print(' = '.join([ x.prettyPrint() for x in varBind ])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/getnext-limit-number-of-variables.py b/examples/hlapi/asyncore/sync/manager/cmdgen/getnext-limit-number-of-variables.py deleted file mode 100644 index f9fd70c1..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/getnext-limit-number-of-variables.py +++ /dev/null @@ -1,42 +0,0 @@ -""" -Fetch fixed amount of MIB variables -+++++++++++++++++++++++++++++++++++ - -Send a series of SNMP GETNEXT requests using the following options: - -* with SNMPv3, user 'usr-sha-aes128', SHA auth, AES128 privacy -* over UDP/IPv4 -* to an Agent at demo.snmplabs.com:161 -* for all OIDs past SNMPv2-MIB -* run till end-of-mib condition is reported by Agent OR maxRows == 100 -* ignoring non-increasing OIDs whenever reported by Agent - -Functionally similar to: - -| $ snmpwalk -v3 -lauthPriv -u usr-sha-aes128 -A authkey1 -X privkey1 -a MD5 -x AES demo.snmplabs.com SNMPv2-MIB::system - -"""# -from pysnmp.hlapi import * - -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in nextCmd(SnmpEngine(), - UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', - authProtocol=usmHMACSHAAuthProtocol, - privProtocol=usmAesCfb128Protocol), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB')), - maxRows=100, ignoreNonIncreasingOid=True): - - if errorIndication: - print(errorIndication) - break - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) - break - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/getnext-multiple-oids-to-eom.py b/examples/hlapi/asyncore/sync/manager/cmdgen/getnext-multiple-oids-to-eom.py deleted file mode 100644 index 863ceb6f..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/getnext-multiple-oids-to-eom.py +++ /dev/null @@ -1,40 +0,0 @@ -""" -Fetch two OID branches -++++++++++++++++++++++ - -Send a series of SNMP GETNEXT requests using the following options: - -* with SNMPv2c, community 'public' -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* for two OIDs in string form -* stop when response OIDs leave the scopes of initial OIDs - -Functionally similar to: - -| $ snmpwalk -v2c -c public demo.snmplabs.com 1.3.6.1.2.1.2.2.1.2 1.3.6.1.2.1.2.2.1.3 - -"""# -from pysnmp.hlapi import * - -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in nextCmd(SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.2')), - ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.3')), - lexicographicMode=False): - - if errorIndication: - print(errorIndication) - break - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex)-1][0] or '?')) - break - else: - for varBind in varBinds: - print(' = '.join([ x.prettyPrint() for x in varBind ])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/multiple-get-calls.py b/examples/hlapi/asyncore/sync/manager/cmdgen/multiple-get-calls.py deleted file mode 100644 index c08b9f47..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/multiple-get-calls.py +++ /dev/null @@ -1,43 +0,0 @@ -""" -Sequence Of GET's -+++++++++++++++++ - -Send two SNMP GET requests in a row 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 IF-MIB::ifInOctets.1 and IF-MIB::ifOutOctets.1 MIB objects - -Use a queue of MIB objects to query. - -The next() call is used to forward Python iterator to the position where it -could consume input - -Functionally similar to: - -| $ snmpget -v3 -l authNoPriv -u usr-md5-none -A authkey1 demo.snmplabs.com IF-MIB::ifInOctets.1 - -"""# -from pysnmp.hlapi import * - -queue = [[ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1))], - [ObjectType(ObjectIdentity('IF-MIB', 'ifOutOctets', 1))]] - -iter = getCmd(SnmpEngine(), - UsmUserData('usr-md5-none', 'authkey1'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData()) - -next(iter) - -while queue: - errorIndication, errorStatus, errorIndex, varBinds = iter.send(queue.pop()) - if errorIndication: - print(errorIndication) - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py b/examples/hlapi/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py deleted file mode 100644 index a9bb2bb3..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py +++ /dev/null @@ -1,38 +0,0 @@ -""" -Preload PySNMP MIBs -+++++++++++++++++++ - -Send a series of SNMP GETNEXT requests using the following options: - -* with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* for all OIDs starting from 1.3.6 -* preload all Python MIB modules found in search path - -Functionally similar to: - -| $ snmpwalk -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 -m ALL demo.snmplabs.com:161 1.3.6 - -"""# -from pysnmp.hlapi import * - -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in nextCmd(SnmpEngine(), - UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('1.3.6').loadMibs())): - - if errorIndication: - print(errorIndication) - break - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) - break - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/pull-whole-mib.py b/examples/hlapi/asyncore/sync/manager/cmdgen/pull-whole-mib.py deleted file mode 100644 index b9103de3..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/pull-whole-mib.py +++ /dev/null @@ -1,37 +0,0 @@ -""" -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 - -Functionally similar to: - -| $ snmpwalk -v3 -lauthPriv -u usr-md5-none -A authkey1 -X privkey1 demo.snmplabs.com IF-MIB:: - -"""# -from pysnmp.hlapi import * - -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in nextCmd(SnmpEngine(), - UsmUserData('usr-md5-none', 'authkey1'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('IF-MIB'))): - - if errorIndication: - print(errorIndication) - break - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) - break - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py b/examples/hlapi/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py deleted file mode 100644 index 91a71a61..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py +++ /dev/null @@ -1,44 +0,0 @@ -""" -Fetch whole SNMP table -++++++++++++++++++++++ - -Send a series of SNMP GETNEXT requests using the following options: - -* with SNMPv1, community 'public' -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* for some columns of the IF-MIB::ifEntry table -* stop when response OIDs leave the scopes of initial OIDs - -Functionally similar to: - -| $ snmpwalk -v1 -c public demo.snmplabs.com IF-MIB::ifDescr IF-MIB::ifType IF-MIB::ifMtu IF-MIB::ifSpeed IF-MIB::ifPhysAddress IF-MIB::ifType - -"""# -from pysnmp.hlapi import * - -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in nextCmd(SnmpEngine(), - CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')), - ObjectType(ObjectIdentity('IF-MIB', 'ifType')), - ObjectType(ObjectIdentity('IF-MIB', 'ifMtu')), - ObjectType(ObjectIdentity('IF-MIB', 'ifSpeed')), - ObjectType(ObjectIdentity('IF-MIB', 'ifPhysAddress')), - ObjectType(ObjectIdentity('IF-MIB', 'ifType')), - lexicographicMode=False): - - if errorIndication: - print(errorIndication) - break - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex)-1][0] or '?')) - break - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py b/examples/hlapi/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py deleted file mode 100644 index 16995ed8..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py +++ /dev/null @@ -1,124 +0,0 @@ -""" -Query Agents from multiple threads -++++++++++++++++++++++++++++++++++ - -Send a bunch of SNMP GET requests simultaneously using the following options: - -* process 5 GET requests in 3 parallel threads -* with SNMPv1, community 'public' and - with SNMPv2c, community 'public' and - with SNMPv3, user 'usr-md5-des', MD5 auth and DES privacy -* over IPv4/UDP and - over IPv6/UDP -* to an Agent at demo.snmplabs.com:161 and - to an Agent at [::1]:161 -* for instances of SNMPv2-MIB::sysDescr.0 and - SNMPv2-MIB::sysLocation.0 MIB objects - -"""# -from sys import version_info -from threading import Thread -from pysnmp.hlapi import * - -if version_info[0] == 2: - from Queue import Queue -else: - from queue import Queue - -# List of targets in the following format: -# ( ( authData, transportTarget, varNames ), ... ) -targets = ( - # 1-st target (SNMPv1 over IPv4/UDP) - (CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 161)), - (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), - # 2-nd target (SNMPv2c over IPv4/UDP) - (CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), - # 3-nd target (SNMPv2c over IPv4/UDP) - same community and - # different transport address. - (CommunityData('public'), - UdpTransportTarget(('localhost', 161)), - (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysContact', 0)), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)))), - # 4-nd target (SNMPv3 over IPv4/UDP) - (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), - # 5-th target (SNMPv3 over IPv6/UDP) - (UsmUserData('usr-md5-none', 'authkey1'), - Udp6TransportTarget(('::1', 161)), - (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), - # N-th target - # ... -) - - -class Worker(Thread): - def __init__(self, requests, responses): - Thread.__init__(self) - self.snmpEngine = SnmpEngine() - self.requests = requests - self.responses = responses - self.setDaemon(True) - self.start() - - def run(self): - while True: - authData, transportTarget, varBinds = self.requests.get() - self.responses.append( - next(getCmd(self.snmpEngine, - authData, transportTarget, ContextData(), *varBinds)) - ) - if hasattr(self.requests, 'task_done'): # 2.5+ - self.requests.task_done() - - -class ThreadPool(object): - def __init__(self, num_threads): - self.requests = Queue(num_threads) - self.responses = [] - for _ in range(num_threads): - Worker(self.requests, self.responses) - - def addRequest(self, authData, transportTarget, varBinds): - self.requests.put((authData, transportTarget, varBinds)) - - def getResponses(self): - return self.responses - - def waitCompletion(self): - if hasattr(self.requests, 'join'): - self.requests.join() # 2.5+ - else: - from time import sleep - # this is a lame substitute for missing .join() - # adding an explicit synchronization might be a better solution - while not self.requests.empty(): - sleep(1) - - -pool = ThreadPool(3) - -# Submit GET requests -for authData, transportTarget, varBinds in targets: - pool.addRequest(authData, transportTarget, varBinds) - -# Wait for responses or errors -pool.waitCompletion() - -# Walk through responses -for errorIndication, errorStatus, errorIndex, varBinds in pool.getResponses(): - if errorIndication: - print(errorIndication) - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py b/examples/hlapi/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py deleted file mode 100644 index b5fc1912..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py +++ /dev/null @@ -1,43 +0,0 @@ -""" -SET scalars values -++++++++++++++++++ - -Send SNMP SET request using the following options: - -* with SNMPv1, community 'public' -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* setting three var-bindings to new values - -Please note, that in this example MIB lookup is only used -for the second var-bindins. For the rest, value types are -inferred from passed objects. - -Functionally similar to: - -| $ snmpset -v1 -c public demo.snmplabs.com 1.3.6.1.2.1.1.9.1.2.1 o 1.3.6.1.4.1.20408.1.1 1.3.6.1.2.1.1.9.1.2.1 = 1.3.6.1.4.1.20408.1.1 1.3.6.1.2.1.1.9.1.3.1 s "new system name" - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - setCmd(SnmpEngine(), - CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.9.1.2.1'), - ObjectIdentifier('1.3.6.1.4.1.20408.1.1')), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.9.1.2.1'), - '1.3.6.1.4.1.20408.1.1'), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.9.1.3.1'), - OctetString('new system name'))) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/specific-v3-engine-id.py b/examples/hlapi/asyncore/sync/manager/cmdgen/specific-v3-engine-id.py deleted file mode 100644 index 94f591c4..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/specific-v3-engine-id.py +++ /dev/null @@ -1,83 +0,0 @@ -""" -Discover SNMPv3 SecurityEngineId -++++++++++++++++++++++++++++++++ - -Send SNMP GET request using the following scenario and options: - -* try to communicate with a SNMPv3 Engine using: - -* a non-existing user -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 - -* if remote SNMP Engine ID is discovered, send SNMP GET request: - -* with SNMPv3, user 'usr-md5-none', MD5 authentication, no privacy - at discovered securityEngineId -* to the same SNMP Engine ID -* for an OID in text form - -"""# -from pysnmp.hlapi import * - -snmpEngine = SnmpEngine() - -transportTarget = UdpTransportTarget(('demo.snmplabs.com', 161)) - -# -# To discover remote SNMP EngineID we will tap on SNMP engine inner workings -# by setting up execution point observer setup on INTERNAL class PDU processing -# - -observerContext = {} - -# Register a callback to be invoked at specified execution point of -# SNMP Engine and passed local variables at execution point's local scope -snmpEngine.observer.registerObserver( - lambda e, p, v, c: c.update(securityEngineId=v['securityEngineId']), - 'rfc3412.prepareDataElements:internal', - cbCtx=observerContext -) - -# Send probe SNMP request with invalid credentials - -authData = UsmUserData('non-existing-user') - -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(snmpEngine, authData, transportTarget, ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))) -) - -# See if our SNMP engine received REPORT PDU containing securityEngineId - -if 'securityEngineId' not in observerContext: - print("Can't discover peer EngineID, errorIndication: %s" % errorIndication) - raise Exception() - -securityEngineId = observerContext.pop('securityEngineId') - -print('Remote securityEngineId = %s' % securityEngineId.prettyPrint()) - -# -# Query remote SNMP Engine using usmUserTable entry configured for it -# - -authData = UsmUserData('usr-md5-none', 'authkey1', - securityEngineId=securityEngineId) - -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(snmpEngine, - authData, - transportTarget, - ContextData(), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'))) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/usm-md5-des.py b/examples/hlapi/asyncore/sync/manager/cmdgen/usm-md5-des.py deleted file mode 100644 index 27c0e7fb..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/usm-md5-des.py +++ /dev/null @@ -1,34 +0,0 @@ -""" -SNMPv3: auth MD5, privacy DES -+++++++++++++++++++++++++++++ - -Send SNMP GET request using the following options: - -* with SNMPv3, user 'usr-md5-des', MD5 authentication, DES encryption -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* for IF-MIB::ifInOctets.1 MIB object - -Functionally similar to: - -| $ snmpget -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 demo.snmplabs.com IF-MIB::ifInOctets.1 - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1))) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/usm-md5-none.py b/examples/hlapi/asyncore/sync/manager/cmdgen/usm-md5-none.py deleted file mode 100644 index f06964e2..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/usm-md5-none.py +++ /dev/null @@ -1,34 +0,0 @@ -""" -SNMPv3: auth MD5, no privacy -++++++++++++++++++++++++++++ - -Send SNMP GET request 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 IF-MIB::ifInOctets.1 MIB object - -Functionally similar to: - -| $ snmpget -v3 -l authNoPriv -u usr-md5-none -A authkey1 demo.snmplabs.com IF-MIB::ifInOctets.1 - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - UsmUserData('usr-md5-none', 'authkey1'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1))) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/usm-none-none.py b/examples/hlapi/asyncore/sync/manager/cmdgen/usm-none-none.py deleted file mode 100644 index 940d8817..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/usm-none-none.py +++ /dev/null @@ -1,33 +0,0 @@ -""" -SNMPv3: no auth, no privacy -+++++++++++++++++++++++++++ - -Send SNMP GET request using the following options: - -* with SNMPv3, user 'usr-none-none', no authentication, no encryption -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* for IF-MIB::ifInOctets.1 MIB object - -Functionally similar to: - -| $ snmpget -v3 -l noAuthNoPriv -u usr-none-none demo.snmplabs.com IF-MIB::ifInOctets.1 - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - UsmUserData('usr-none-none'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1))) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/usm-sha-aes128.py b/examples/hlapi/asyncore/sync/manager/cmdgen/usm-sha-aes128.py deleted file mode 100644 index 53d4f800..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/usm-sha-aes128.py +++ /dev/null @@ -1,55 +0,0 @@ -""" -SNMPv3: auth SHA, privacy AES128 -++++++++++++++++++++++++++++++++ - -Send SNMP GET request using the following options: - -* with SNMPv3, user 'usr-sha-aes', SHA authentication, AES128 encryption -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* for SNMPv2-MIB::sysDescr.0 MIB object - -Available authentication protocols: - -#. usmHMACMD5AuthProtocol -#. usmHMACSHAAuthProtocol -#. usmHMAC128SHA224AuthProtocol -#. usmHMAC192SHA256AuthProtocol -#. usmHMAC256SHA384AuthProtocol -#. usmHMAC384SHA512AuthProtocol -#. usmNoAuthProtocol - -Available privacy protocols: - -#. usmDESPrivProtocol -#. usm3DESEDEPrivProtocol -#. usmAesCfb128Protocol -#. usmAesCfb192Protocol -#. usmAesCfb256Protocol -#. usmNoPrivProtocol - -Functionally similar to: - -| $ snmpget -v3 -l authPriv -u usr-sha-aes -A authkey1 -X privkey1 -a SHA -x AES demo.snmplabs.com SNMPv2-MIB::sysDescr.0 - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - UsmUserData('usr-sha-aes', 'authkey1', 'privkey1', - authProtocol=usmHMACSHAAuthProtocol, - privProtocol=usmAesCfb128Protocol), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/v1-get.py b/examples/hlapi/asyncore/sync/manager/cmdgen/v1-get.py deleted file mode 100644 index 1b272967..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/v1-get.py +++ /dev/null @@ -1,34 +0,0 @@ -""" -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, - -Functionally similar to: - -| $ snmpget -v1 -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/v2c-get.py b/examples/hlapi/asyncore/sync/manager/cmdgen/v2c-get.py deleted file mode 100644 index 606bab5b..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/v2c-get.py +++ /dev/null @@ -1,35 +0,0 @@ -""" -SNMPv2c -+++++++ - -Send SNMP GET request using the following options: - -* with SNMPv2c, community 'public' -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* for two OIDs in string form - -Functionally similar to: - -| $ snmpget -v2c -c public demo.snmplabs.com 1.3.6.1.2.1.1.1.0 1.3.6.1.2.1.1.6.0 - -"""# -from pysnmp.hlapi import * - -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0')), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.6.0'))) -) - -if errorIndication: - print(errorIndication) -elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) -else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/v2c-getbulk.py b/examples/hlapi/asyncore/sync/manager/cmdgen/v2c-getbulk.py deleted file mode 100644 index 47715195..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/v2c-getbulk.py +++ /dev/null @@ -1,42 +0,0 @@ -""" -Bulk walk MIB -+++++++++++++ - -Send a series of SNMP GETBULK requests using the following options: - -* with SNMPv2c, community 'public' -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* with values non-repeaters = 0, max-repetitions = 25 -* for two OIDs in string form -* stop when response OIDs leave the scopes of initial OIDs - -Functionally similar to: - -| $ snmpbulkwalk -v2c -c public demo.snmplabs.com -Cn0 -Cr25 1.3.6.1.2.1.2.2 1.3.6.1.2.1.2.3 - -"""# -from pysnmp.hlapi import * - -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in bulkCmd(SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - 0, 25, - ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2')), - ObjectType(ObjectIdentity('1.3.6.1.2.1.2.3')), - lexicographicMode=False): - - if errorIndication: - print(errorIndication) - break - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) - break - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/waive-mib-lookup.py b/examples/hlapi/asyncore/sync/manager/cmdgen/waive-mib-lookup.py deleted file mode 100644 index 17194552..00000000 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/waive-mib-lookup.py +++ /dev/null @@ -1,43 +0,0 @@ -""" -Waive MIB lookup -++++++++++++++++ - -Perform SNMP GETNEXT operation with the following options: - -* with SNMPv2c, community 'public' -* over IPv4/UDP -* to an Agent at demo.snmplabs.com:161 -* for an OID in string form -* do not resolve response OIDs and values into human-freidly form - -False lookupMib keyword arguments could make pysnmp waiving -OIDs and values resolution in response variable-bindings, into human -friendly form. - -Functionally similar to: - -| $ snmpwalk -v2c -c public -ObentU demo.snmplabs.com 1.3.6.1.2.1 - -"""# -from pysnmp.hlapi import * - -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in nextCmd(SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1')), - lookupMib=False): - - if errorIndication: - print(errorIndication) - break - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) - break - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/trollius/agent/ntforg/default-v1-trap.py b/examples/hlapi/trollius/agent/ntforg/default-v1-trap.py deleted file mode 100644 index 514cff2f..00000000 --- a/examples/hlapi/trollius/agent/ntforg/default-v1-trap.py +++ /dev/null @@ -1,62 +0,0 @@ -""" -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' -* use trollius I/O framework - -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" - -"""# -import trollius -from pysnmp.hlapi.asyncio import * - - -@trollius.coroutine -def run(): - snmpEngine = SnmpEngine() - (errorIndication, - errorStatus, - errorIndex, - varBinds) = yield trollius.From( - sendNotification( - snmpEngine, - CommunityData('public'), # mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData(), - 'inform', - 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')) - ) - ) - ) - - if errorIndication: - print(errorIndication) - elif errorStatus: - print('%s: at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) - - snmpEngine.transportDispatcher.closeDispatcher() - - -trollius.get_event_loop().run_until_complete(run()) diff --git a/examples/hlapi/trollius/manager/cmdgen/getbulk-to-eom.py b/examples/hlapi/trollius/manager/cmdgen/getbulk-to-eom.py deleted file mode 100644 index b380b283..00000000 --- a/examples/hlapi/trollius/manager/cmdgen/getbulk-to-eom.py +++ /dev/null @@ -1,61 +0,0 @@ -""" -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 trollius I/O framework - -Functionally similar to: - -| $ snmpbulkwalk -v3 -lnoAuthNoPriv -u usr-none-none -Cn0 -Cr50 demo.snmplabs.com SNMPv2-MIB::system - -"""# -import trollius -from pysnmp.hlapi.asyncio import * - - -@trollius.coroutine -def run(varBinds): - snmpEngine = SnmpEngine() - while True: - (errorIndication, - errorStatus, - errorIndex, - varBindTable) = yield trollius.From( - bulkCmd(snmpEngine, - UsmUserData('usr-none-none'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - 0, 50, - *varBinds) - ) - - if errorIndication: - print(errorIndication) - break - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) - else: - for varBindRow in varBindTable: - for varBind in varBindRow: - print(' = '.join([x.prettyPrint() for x in varBind])) - - varBinds = varBindTable[-1] - if isEndOfMib(varBinds): - break - - snmpEngine.transportDispatcher.closeDispatcher() - - -loop = trollius.get_event_loop() - -loop.run_until_complete( - run([ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr'))]) -) diff --git a/examples/hlapi/trollius/manager/cmdgen/v1-get.py b/examples/hlapi/trollius/manager/cmdgen/v1-get.py deleted file mode 100644 index d7406963..00000000 --- a/examples/hlapi/trollius/manager/cmdgen/v1-get.py +++ /dev/null @@ -1,48 +0,0 @@ -""" -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 an instance of SNMPv2-MIB::sysDescr.0 MIB object - * Based on trollius I/O framework - -Functionally similar to: - -| $ snmpget -v1 -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 - -"""# -import trollius -from pysnmp.hlapi.asyncio import * - - -@trollius.coroutine -def run(): - snmpEngine = SnmpEngine() - (errorIndication, - errorStatus, - errorIndex, - varBinds) = yield trollius.From( - getCmd(snmpEngine, - CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))) - ) - - if errorIndication: - print(errorIndication) - elif errorStatus: - print('%s at %s' % (errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) - else: - for varBind in varBinds: - print(' = '.join([x.prettyPrint() for x in varBind])) - - snmpEngine.transportDispatcher.closeDispatcher() - - -trollius.get_event_loop().run_until_complete(run()) diff --git a/examples/hlapi/twisted/agent/ntforg/default-v1-trap.py b/examples/hlapi/twisted/agent/ntforg/default-v1-trap.py deleted file mode 100644 index 4d1a2700..00000000 --- a/examples/hlapi/twisted/agent/ntforg/default-v1-trap.py +++ /dev/null @@ -1,65 +0,0 @@ -""" -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(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') - ).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 deleted file mode 100644 index e9d199e9..00000000 --- a/examples/hlapi/twisted/agent/ntforg/multiple-notifications-at-once.py +++ /dev/null @@ -1,75 +0,0 @@ -""" -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.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') - ).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/twisted/agent/ntforg/v2c-trap-inline-callbacks.py b/examples/hlapi/twisted/agent/ntforg/v2c-trap-inline-callbacks.py deleted file mode 100644 index e326108c..00000000 --- a/examples/hlapi/twisted/agent/ntforg/v2c-trap-inline-callbacks.py +++ /dev/null @@ -1,44 +0,0 @@ -""" -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.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') - ).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/twisted/manager/cmdgen/custom-timeout-and-retries.py b/examples/hlapi/twisted/manager/cmdgen/custom-timeout-and-retries.py deleted file mode 100644 index a1ff7850..00000000 --- a/examples/hlapi/twisted/manager/cmdgen/custom-timeout-and-retries.py +++ /dev/null @@ -1,53 +0,0 @@ -""" -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(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/twisted/manager/cmdgen/getbulk-to-eom.py b/examples/hlapi/twisted/manager/cmdgen/getbulk-to-eom.py deleted file mode 100644 index d3675451..00000000 --- a/examples/hlapi/twisted/manager/cmdgen/getbulk-to-eom.py +++ /dev/null @@ -1,54 +0,0 @@ -""" -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(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/twisted/manager/cmdgen/multiple-concurrent-queries.py b/examples/hlapi/twisted/manager/cmdgen/multiple-concurrent-queries.py deleted file mode 100644 index 07fb08ea..00000000 --- a/examples/hlapi/twisted/manager/cmdgen/multiple-concurrent-queries.py +++ /dev/null @@ -1,60 +0,0 @@ -""" -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(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/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 deleted file mode 100644 index 41d779e6..00000000 --- a/examples/hlapi/twisted/manager/cmdgen/pull-mibs-from-multiple-agents-at-once.py +++ /dev/null @@ -1,64 +0,0 @@ -""" -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(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/twisted/manager/cmdgen/pull-whole-mib.py b/examples/hlapi/twisted/manager/cmdgen/pull-whole-mib.py deleted file mode 100644 index b2b245e6..00000000 --- a/examples/hlapi/twisted/manager/cmdgen/pull-whole-mib.py +++ /dev/null @@ -1,52 +0,0 @@ -""" -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(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/twisted/manager/cmdgen/v1-get.py b/examples/hlapi/twisted/manager/cmdgen/v1-get.py deleted file mode 100644 index 21719ff7..00000000 --- a/examples/hlapi/twisted/manager/cmdgen/v1-get.py +++ /dev/null @@ -1,51 +0,0 @@ -""" -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(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']) diff --git a/examples/hlapi/v1arch/asyncore/agent/ntforg/generic-v1-trap.py b/examples/hlapi/v1arch/asyncore/agent/ntforg/generic-v1-trap.py new file mode 100644 index 00000000..ca969f10 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/agent/ntforg/generic-v1-trap.py @@ -0,0 +1,52 @@ +""" +Generic SNMPv1 TRAP ++++++++++++++++++++ + +Send SNMPv1 TRAP using the following options: + +* SNMPv1 +* with community name 'public' +* over IPv4/UDP +* send TRAP notification +* with Uptime 12345 +* with Generic Trap #1 (warmStart) and Specific Trap 0 +* with Agent Address 127.0.0.1 +* 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 pysnmp.hlapi.v1arch.asyncore import * + + +def cbFun(errorIndication, errorStatus, errorIndex, varBinds, **context): + if errorIndication: + print(errorIndication) + + +snmpDispatcher = SnmpDispatcher() + +sendNotification( + snmpDispatcher, + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 162)), + 'trap', + # SNMPv2-MIB::sysUpTime.0 = 12345 + ('1.3.6.1.2.1.1.3.0', TimeTicks(12345)), + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::warmStart + ('1.3.6.1.6.3.1.1.4.1.0', ObjectIdentifier('1.3.6.1.6.3.1.1.5.2')), + # SNMP-COMMUNITY-MIB::snmpTrapAddress.0 = 127.0.0.1 + ('1.3.6.1.6.3.18.1.3.0', IpAddress('127.0.0.1')), + # SNMP-COMMUNITY-MIB::snmpTrapCommunity.0 = public + ('1.3.6.1.6.3.18.1.4.0', OctetString('public')), + # SNMP-COMMUNITY-MIB::snmpTrapEnterprise.0 = 1.3.6.1.4.1.20408.4.1.1.2 + ('1.3.6.1.6.3.1.1.4.3.0', ObjectIdentifier('1.3.6.1.4.1.20408.4.1.1.2')), + # SNMPv2-MIB::sysName.0 + ('1.3.6.1.2.1.1.1.0', OctetString('my system')), + cbFun=cbFun +) + +snmpDispatcher.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v1arch/asyncore/agent/ntforg/generic-v2c-trap.py b/examples/hlapi/v1arch/asyncore/agent/ntforg/generic-v2c-trap.py new file mode 100644 index 00000000..36c01e60 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/agent/ntforg/generic-v2c-trap.py @@ -0,0 +1,44 @@ +""" +Generic SNMPv2c TRAP +++++++++++++++++++++ + +Send SNMPv1 TRAP using the following options: + +* SNMPv2c +* with community name 'public' +* over IPv4/UDP +* send TRAP notification +* with Uptime 12345 +* 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 "my system" + +"""# +from pysnmp.hlapi.v1arch.asyncore import * + + +def cbFun(errorIndication, errorStatus, errorIndex, varBinds, **context): + if errorIndication: + print(errorIndication) + + +snmpDispatcher = SnmpDispatcher() + +sendNotification( + snmpDispatcher, + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + 'trap', + # SNMPv2-MIB::sysUpTime.0 = 12345 + ('1.3.6.1.2.1.1.3.0', TimeTicks(12345)), + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::warmStart + ('1.3.6.1.6.3.1.1.4.1.0', ObjectIdentifier('1.3.6.1.6.3.1.1.5.2')), + # SNMPv2-MIB::sysName.0 + ('1.3.6.1.2.1.1.1.0', OctetString('my system')), + cbFun=cbFun +) + +snmpDispatcher.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v1arch/asyncore/agent/ntforg/v2c-inform.py b/examples/hlapi/v1arch/asyncore/agent/ntforg/v2c-inform.py new file mode 100644 index 00000000..8271a9f5 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/agent/ntforg/v2c-inform.py @@ -0,0 +1,51 @@ +""" +SNMPv2c INFORM +++++++++++++++ + +Send SNMP INFORM notification using the following options: + +* SNMPv2c +* with community name 'public' +* over IPv4/UDP +* send INFORM notification +* with TRAP ID 'warmStart' specified as a string OID +* include managed object information 1.3.6.1.2.1.1.5.0 = 'system name' + +Functionally similar to: + +| $ snmpinform -v2c -c public demo.snmplabs.com 12345 1.3.6.1.4.1.20408.4.1.1.2 1.3.6.1.2.1.1.1.0 s "my system" + +"""# +from pysnmp.hlapi.v1arch.asyncore import * + + +def cbFun(errorIndication, errorStatus, errorIndex, varBinds, **context): + if errorIndication: + print(errorIndication) + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) + + +snmpDispatcher = SnmpDispatcher() + +sendNotification( + snmpDispatcher, + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + 'inform', + # SNMPv2-MIB::sysUpTime.0 = 12345 + ('1.3.6.1.2.1.1.3.0', TimeTicks(12345)), + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::warmStart + ('1.3.6.1.6.3.1.1.4.1.0', ObjectIdentifier('1.3.6.1.6.3.1.1.5.2')), + # SNMPv2-MIB::sysName.0 + ('1.3.6.1.2.1.1.1.0', OctetString('my system')), + cbFun=cbFun +) + +snmpDispatcher.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v1arch/asyncore/agent/ntforg/v2c-trap-with-notification-objects.py b/examples/hlapi/v1arch/asyncore/agent/ntforg/v2c-trap-with-notification-objects.py new file mode 100644 index 00000000..a3bc18ae --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/agent/ntforg/v2c-trap-with-notification-objects.py @@ -0,0 +1,53 @@ +""" +Generic SNMPv2c TRAP +++++++++++++++++++++ + +Send SNMPv1 TRAP using the following options: + +* SNMPv2c +* with community name 'public' +* over IPv4/UDP +* send TRAP notification +* with Uptime 12345 +* 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 "my system" + +"""# +from pysnmp.hlapi.v1arch.asyncore import * + + +def cbFun(errorIndication, errorStatus, errorIndex, varBinds, **context): + if errorIndication: + print(errorIndication) + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) + + +snmpDispatcher = SnmpDispatcher() + +sendNotification( + snmpDispatcher, + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + 'trap', + # SNMPv2-MIB::sysUpTime.0 = 12345 + ('1.3.6.1.2.1.1.3.0', TimeTicks(12345)), + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::warmStart + NotificationType(ObjectIdentity('SNMPv2-MIB', 'warmStart')), + # SNMPv2-MIB::sysName.0 + ('1.3.6.1.2.1.1.1.0', OctetString('my system')), + cbFun=cbFun, + lookupMib=True +) + +snmpDispatcher.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v1arch/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py b/examples/hlapi/v1arch/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py new file mode 100644 index 00000000..7c282a81 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py @@ -0,0 +1,63 @@ +""" +Multiple concurrent queries ++++++++++++++++++++++++++++ + +Send a bunch of different SNMP GET requests to different peers all at once, +wait for responses asynchronously: + +* with SNMPv1, community 'public' and + with SNMPv2c, community 'public' and +* over IPv4/UDP and + over IPv6/UDP +* to an Agent at demo.snmplabs.com:161 and + to an Agent at [::1]:161 +* for instances of SNMPv2-MIB::system + SNMPv2-MIB::sysLocation.0 MIB objects +* Enable MIB lookup feature +"""# +from pysnmp.hlapi.v1arch.asyncore import * + +# List of targets in the following format: +# ((authData, transportTarget, varNames), ...) +targets = ( + # 1-st target (SNMPv1 over IPv4/UDP) + (CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # 2-nd target (SNMPv2c over IPv4/UDP) + (CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # 3-nd target (SNMPv2c over IPv4/UDP) - same community and + # different transport address. + (CommunityData('public'), + Udp6TransportTarget(('::1', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysContact', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)))), + # N-th target + # ... +) + + +def cbFun(errorIndication, errorStatus, errorIndex, varBinds, **context): + if errorIndication: + print(errorIndication) + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) + +snmpDispatcher = SnmpDispatcher() + +# Submit a bunch of initial GET requests +for authData, transportTarget, varBinds in targets: + getCmd(snmpDispatcher, authData, transportTarget, *varBinds, + **dict(cbFun=cbFun, lookupMib=True)) + +snmpDispatcher.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py b/examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py new file mode 100644 index 00000000..eaf1755d --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py @@ -0,0 +1,63 @@ +""" +Walk multiple Agents at once +++++++++++++++++++++++++++++ + +Iterate over MIBs of multiple SNMP Agents asynchronously using the +following options: + +* with SNMPv1, community 'public' and + with SNMPv2c, community 'public' and +* over IPv4/UDP and + over IPv6/UDP +* to an Agent at demo.snmplabs.com:161 and + to an Agent at [::1]:161 +* pull MIB variables till EOM +* Enable MIB lookup feature + +"""# +from pysnmp.hlapi.v1arch.asyncore import * + +# List of targets in the following format: +# ((authData, transportTarget, varNames), ...) +targets = ( + # 1-st target (SNMPv1 over IPv4/UDP) + (CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + (ObjectType(ObjectIdentity('1.3.6.1.2.1')), + ObjectType(ObjectIdentity('1.3.6.1.3.1')))), + # 2-nd target (SNMPv2c over IPv4/UDP) + (CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + (ObjectType(ObjectIdentity('1.3.6.1.4.1')),)), + # 3-th target (SNMPv3 over IPv6/UDP) + (CommunityData('public'), + Udp6TransportTarget(('::1', 161)), + (ObjectType(ObjectIdentity('IF-MIB', 'ifTable')),)) + # N-th target + # ... +) + + +def cbFun(errorIndication, errorStatus, errorIndex, varBindTable, **context): + if errorIndication: + print(errorIndication) + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + + else: + for varBindRow in varBindTable: + for varBind in varBindRow: + print(' = '.join([x.prettyPrint() for x in varBind])) + + return context.get('nextVarBinds') + +snmpDispatcher = SnmpDispatcher() + +# Submit a bunch of initial GETNEXT requests +for authData, transportTarget, varBinds in targets: + nextCmd(snmpDispatcher, authData, transportTarget, *varBinds, + **dict(cbFun=cbFun, lookupMib=True)) + +snmpDispatcher.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-whole-mib.py b/examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-whole-mib.py new file mode 100644 index 00000000..c5731570 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-whole-mib.py @@ -0,0 +1,44 @@ +""" +Walk whole MIB +++++++++++++++ + +Send a series of SNMP GETBULK requests using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for all OIDs past 1.3.6.1.4.1 + +Functionally similar to: + +| $ snmpwalk -v2c -c public demo.snmplabs.com 1.3.6.1.4.1 +"""# +from pysnmp.hlapi.v1arch.asyncore import * + + +def cbFun(errorIndication, errorStatus, errorIndex, varBindTable, **context): + if errorIndication: + print(errorIndication) + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + + else: + for varBindRow in varBindTable: + for varBind in varBindRow: + print(' = '.join([x.prettyPrint() for x in varBind])) + + return context.get('nextVarBinds') + +snmpDispatcher = SnmpDispatcher() + +# Submit initial GETBULK request +bulkCmd(snmpDispatcher, + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + 0, 25, + ('1.3.6.1.4.1', None), + **dict(cbFun=cbFun)) + +snmpDispatcher.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v1arch/asyncore/manager/cmdgen/v1-getnext.py b/examples/hlapi/v1arch/asyncore/manager/cmdgen/v1-getnext.py new file mode 100644 index 00000000..385e869c --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/manager/cmdgen/v1-getnext.py @@ -0,0 +1,48 @@ +""" +SNMP GETNEXT over SNMPv1 +++++++++++++++++++++++++ + +Send a series of SNMP GETNEXT requests using the following options: + + * with SNMPv1, community 'public' + * over IPv4/UDP + * to an Agent at demo.snmplabs.com:161 + * for the 1.3.6.1.2.1.1 OID (e.g. SNMPv2-MIB::system MIB branch) + +Functionally similar to: + +| $ snmpwalk -v1 -c public demo.snmplabs.com 1.3.6.1.2.1.1 + +"""# + +from pysnmp.hlapi.v1arch.asyncore import * + + +def cbFun(errorIndication, errorStatus, errorIndex, varBindTable, **context): + if errorIndication: + print(errorIndication) + return + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBindTable[-1][int(errorIndex) - 1][0] or '?')) + return + + else: + for varBindRow in varBindTable: + for varBind in varBindRow: + print(' = '.join([x.prettyPrint() for x in varBind])) + + return context.get('nextVarBinds') + +snmpDispatcher = SnmpDispatcher() + +stateHandle = nextCmd( + snmpDispatcher, + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ('1.3.6.1.5.1.1', None), + cbFun=cbFun +) + +snmpDispatcher.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v1arch/asyncore/manager/cmdgen/v2c-get.py b/examples/hlapi/v1arch/asyncore/manager/cmdgen/v2c-get.py new file mode 100644 index 00000000..9ce8d7dd --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/manager/cmdgen/v2c-get.py @@ -0,0 +1,44 @@ +""" +SNMP GET over SNMPv2c ++++++++++++++++++++++ + +Send SNMP GET request using the following options: + + * with SNMPv2c, community 'public' + * over IPv4/UDP + * to an Agent at demo.snmplabs.com:161 + * for the 1.3.6.1.2.1.1.1.0 OID (e.g. SNMPv2-MIB::sysDescr.0 MIB object) + +Functionally similar to: + +| $ snmpget -v2c -c public demo.snmplabs.com 1.3.6.1.2.1.1.1.0 + +"""# + +from pysnmp.hlapi.v1arch.asyncore import * + + +def cbFun(errorIndication, errorStatus, errorIndex, varBinds, **context): + if errorIndication: + print(errorIndication) + return + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBindTable[-1][int(errorIndex) - 1][0] or '?')) + return + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) + + +snmpDispatcher = SnmpDispatcher() + +stateHandle = getCmd( + snmpDispatcher, + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ('1.3.6.1.2.1.1.1.0', None), + cbFun=cbFun +) + +snmpDispatcher.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v1-trap.py b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v1-trap.py new file mode 100644 index 00000000..2f1fa0fd --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v1-trap.py @@ -0,0 +1,46 @@ +""" +Generic SNMPv1 TRAP ++++++++++++++++++++ + +Send SNMPv1 TRAP using the following options: + +* SNMPv1 +* with community name 'public' +* over IPv4/UDP +* send TRAP notification +* with Uptime 12345 +* with Generic Trap #1 (warmStart) and Specific Trap 0 +* with Agent Address 127.0.0.1 +* 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 pysnmp.hlapi.v1arch import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + sendNotification( + SnmpDispatcher(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 162)), + 'trap', + # SNMPv2-MIB::sysUpTime.0 = 12345 + ('1.3.6.1.2.1.1.3.0', TimeTicks(12345)), + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::warmStart + ('1.3.6.1.6.3.1.1.4.1.0', ObjectIdentifier('1.3.6.1.6.3.1.1.5.2')), + # SNMP-COMMUNITY-MIB::snmpTrapAddress.0 = 127.0.0.1 + ('1.3.6.1.6.3.18.1.3.0', IpAddress('127.0.0.1')), + # SNMP-COMMUNITY-MIB::snmpTrapCommunity.0 = public + ('1.3.6.1.6.3.18.1.4.0', OctetString('public')), + # SNMP-COMMUNITY-MIB::snmpTrapEnterprise.0 = 1.3.6.1.4.1.20408.4.1.1.2 + ('1.3.6.1.6.3.1.1.4.3.0', ObjectIdentifier('1.3.6.1.4.1.20408.4.1.1.2')), + # SNMPv2-MIB::sysName.0 + ('1.3.6.1.2.1.1.1.0', OctetString('my system')) + ) +) + +if errorIndication: + print(errorIndication) diff --git a/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v2c-trap.py b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v2c-trap.py new file mode 100644 index 00000000..10a04ed4 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v2c-trap.py @@ -0,0 +1,38 @@ +""" +Generic SNMPv2c TRAP +++++++++++++++++++++ + +Send SNMPv1 TRAP using the following options: + +* SNMPv2c +* with community name 'public' +* over IPv4/UDP +* send TRAP notification +* with Uptime 12345 +* 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 "my system" + +"""# +from pysnmp.hlapi.v1arch import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + sendNotification( + SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + 'trap', + # SNMPv2-MIB::sysUpTime.0 = 12345 + ('1.3.6.1.2.1.1.3.0', TimeTicks(12345)), + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::warmStart + ('1.3.6.1.6.3.1.1.4.1.0', ObjectIdentifier('1.3.6.1.6.3.1.1.5.2')), + # SNMPv2-MIB::sysName.0 + ('1.3.6.1.2.1.1.1.0', OctetString('my system')) + ) +) + +if errorIndication: + print(errorIndication) diff --git a/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-inform.py b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-inform.py new file mode 100644 index 00000000..c1799993 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-inform.py @@ -0,0 +1,45 @@ +""" +SNMPv2c INFORM +++++++++++++++ + +Send SNMP INFORM notification using the following options: + +* SNMPv2c +* with community name 'public' +* over IPv4/UDP +* send INFORM notification +* with TRAP ID 'warmStart' specified as a string OID +* include managed object information 1.3.6.1.2.1.1.5.0 = 'system name' + +Functionally similar to: + +| $ snmpinform -v2c -c public demo.snmplabs.com 12345 1.3.6.1.4.1.20408.4.1.1.2 1.3.6.1.2.1.1.1.0 s "my system" + +"""# +from pysnmp.hlapi.v1arch import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + sendNotification( + SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + 'inform', + # SNMPv2-MIB::sysUpTime.0 = 12345 + ('1.3.6.1.2.1.1.3.0', TimeTicks(12345)), + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::warmStart + ('1.3.6.1.6.3.1.1.4.1.0', ObjectIdentifier('1.3.6.1.6.3.1.1.5.2')), + # SNMPv2-MIB::sysName.0 + ('1.3.6.1.2.1.1.1.0', OctetString('my system')) + ) +) + +if errorIndication: + print(errorIndication) + +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py new file mode 100644 index 00000000..052946bc --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py @@ -0,0 +1,39 @@ +""" +Generic SNMPv2c TRAP +++++++++++++++++++++ + +Send SNMPv1 TRAP using the following options: + +* SNMPv2c +* with community name 'public' +* over IPv4/UDP +* send TRAP notification +* with Uptime 12345 +* 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 "my system" + +"""# +from pysnmp.hlapi.v1arch import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + sendNotification( + SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + 'trap', + # SNMPv2-MIB::sysUpTime.0 = 12345 + ('1.3.6.1.2.1.1.3.0', TimeTicks(12345)), + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::warmStart + NotificationType(ObjectIdentity('SNMPv2-MIB', 'warmStart')), + # SNMPv2-MIB::sysName.0 + ('1.3.6.1.2.1.1.1.0', OctetString('my system')), + lookupMib=True + ) +) + +if errorIndication: + print(errorIndication) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py new file mode 100644 index 00000000..a6789f7f --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py @@ -0,0 +1,38 @@ +""" +Coerce value to SET to MIB spec ++++++++++++++++++++++++++++++++ + +Send SNMP SET request using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* with MIB lookup enabled +* setting SNMPv2-MIB::sysName.0 to new value (type taken from MIB) + +Functionally similar to: + +| $ snmpset -v2c -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 = "new system name" + +"""# +from pysnmp.hlapi.v1arch import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + setCmd(SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysORDescr', 1), + 'new description'), + lookupMib=True) +) + +if errorIndication: + print(errorIndication) + +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py new file mode 100644 index 00000000..759171c1 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py @@ -0,0 +1,40 @@ +""" +Custom ASN.1 MIB path ++++++++++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for IF-MIB::ifInOctets.1 MIB object +* pass non-default ASN.1 MIB source to MIB compiler +* with MIB lookup enabled + +Functionally similar to: + +| $ snmpget -v2c -c public -M /usr/share/snmp demo.snmplabs.com IF-MIB::ifInOctets.1 + +"""# +from pysnmp.hlapi.v1arch import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1).addAsn1MibSource( + 'file:///usr/share/snmp', + 'http://mibs.snmplabs.com/asn1/@mib@')), + lookupMib=True) +) + +if errorIndication: + print(errorIndication) + +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py new file mode 100644 index 00000000..a3686d69 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py @@ -0,0 +1,49 @@ +""" +Custom PySNMP MIBs location ++++++++++++++++++++++++++++ + +Send a series of SNMP GETBULK requests using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for all OIDs within TCP-MIB::tcpConnTable column +* TCP-MIB Python module will be searched by a user-specified filesystem + path (/opt/mib/pysnmp) and in Python package (python_packaged_mibs) + which should be in sys.path +* with MIB lookup enabled + +Functionally similar to: + +| $ snmpbulkwalk -v2c -c public -Cn0 -Cr50 demo.snmplabs.com TCP-MIB::tcpConnTable + +"""# +from pysnmp.hlapi.v1arch import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in bulkCmd(SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + 0, 50, + ObjectType( + ObjectIdentity('TCP-MIB', 'tcpConnTable').addMibSource( + '/opt/mibs/pysnmp').addMibSource( + 'python_packaged_mibs') + ), + lookupMib=True, + lexicographicMode=False): + + if errorIndication: + print(errorIndication) + break + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + break + + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py new file mode 100644 index 00000000..7a4253cf --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py @@ -0,0 +1,51 @@ +""" +Custom request timeout +++++++++++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for an OID in string form +* use custom timeout and request retries values + +Transport timing settings (maximum number of request retries and +individual request timeout in seconds) can be set on a per-target basis +as explained by the code that follows. + +Keep in mind that while timeout value can be specified in fractions of a +second, default pysnmp timer resolution is quite low (about 0.5 sec) +so there's no much point in using timeouts which is not a multiple of 0.5 +Internal timer can be programmatically adjusted to finer resolution if needed. + +If retries value is set to 0, pysnmp will issue a single request. Even +if no response arrives, there will be no retry. Likewise, retries=1 +means one initial request plus one retry. + +Functionally similar to: + +| $ snmpget -v1 -c public -t 2 -r 0 demo.snmplabs.com 1.3.6.1.2.1.1.1.0 + +"""# +from pysnmp.hlapi.v1arch import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget( + ('demo.snmplabs.com', 161), timeout=2.0, retries=0 + ), + (('1.3.6.1.2.1.1.1.0', None))) +) + +if errorIndication: + print(errorIndication) + +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/enable-mib-lookup.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/enable-mib-lookup.py new file mode 100644 index 00000000..2f4f8edb --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/enable-mib-lookup.py @@ -0,0 +1,43 @@ +""" +Enable MIB lookup ++++++++++++++++++ + +Perform SNMP GETNEXT operation with the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for an OID in string form +* resolve request and response OIDs and values from/to human-friendly form + +The `lookupMib=True` keyword argument makes pysnmp resolving +request and response variable-bindings from/to human-friendly form. + +Functionally similar to: + +| $ snmpwalk -v2c -c public -ObentU demo.snmplabs.com 1.3.6.1.2.1 + +"""# +from pysnmp.hlapi.v1arch import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in nextCmd(SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1')), + lookupMib=True): + + if errorIndication: + print(errorIndication) + break + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + break + + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py new file mode 100644 index 00000000..bd8d9ca2 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py @@ -0,0 +1,36 @@ +""" +GET over IPv6 ++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv2c, community 'public' +* over IPv6/UDP +* to an Agent at [::1]:161 +* for three OIDs in string form + +Functionally similar to: + +| $ snmpget -v2c -c public udp6:[::1]:161 1.3.6.1.2.1.1.1.0 1.3.6.1.2.1.1.2.0 1.3.6.1.2.1.1.3.0 +"""# +from pysnmp.hlapi.v1arch import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpDispatcher(), + CommunityData('public'), + Udp6TransportTarget(('::1', 161)), + ('1.3.6.1.2.1.1.1.0', None), + ('1.3.6.1.2.1.1.2.0', None), + ('1.3.6.1.2.1.1.3.0', None)) +) + +if errorIndication: + print(errorIndication) + +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py new file mode 100644 index 00000000..b7fccdcb --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py @@ -0,0 +1,38 @@ +""" +GET table row ++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv2c, community name "public" +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* with MIB lookup enabled +* for IF-MIB::ifInOctets.1 and IF-MIB::ifOutOctets.1 MIB object + +Functionally similar to: + +| $ snmpget -v 2c -c public demo.snmplabs.com IF-MIB::ifInOctets.1 IF-MIB::ifOutOctets.1 + +"""# +from pysnmp.hlapi.v1arch import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1)), + ObjectType(ObjectIdentity('IF-MIB', 'ifOutOctets', 1)), + lookupMib=True) +) + +if errorIndication: + print(errorIndication) + +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py new file mode 100644 index 00000000..cec5ad25 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py @@ -0,0 +1,40 @@ +""" +Fetch table row by composite index +++++++++++++++++++++++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for TCP-MIB::tcpConnLocalAddress."0.0.0.0".22."0.0.0.0".0 MIB object +* with MIB lookup enabled + +Functionally similar to: + +| $ snmpget -v2c -c public demo.snmplabs.com TCP-MIB::tcpConnLocalAddress."0.0.0.0".22."0.0.0.0".0 + +"""# +from pysnmp.hlapi.v1arch import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType(ObjectIdentity('TCP-MIB', + 'tcpConnLocalAddress', + '0.0.0.0', 22, + '0.0.0.0', 0)), + lookupMib=True) +) + +if errorIndication: + print(errorIndication) + +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py new file mode 100644 index 00000000..49257a1c --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py @@ -0,0 +1,45 @@ +""" +Fetch scalar and table variables +++++++++++++++++++++++++++++++++ + +Send a series of SNMP GETBULK requests using the following options: + +* with SNMPv2c, community name "public" +* over IPv6/UDP +* to an Agent at demo.snmplabs.com:161 +* with MIB lookup enabled +* with values non-repeaters = 1, max-repetitions = 25 +* for IP-MIB::ipAdEntAddr and all columns of the IF-MIB::ifEntry table +* stop when response OIDs leave the scopes of the table + +Functionally similar to: + +| $ snmpbulkwalk -v2c -c public -Cn1, -Cr25 demo.snmplabs.com IP-MIB::ipAdEntAddr IP-MIB::ipAddrEntry + +"""# +from pysnmp.hlapi.v1arch import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in bulkCmd(SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + 1, 25, + ObjectType(ObjectIdentity('IP-MIB', 'ipAdEntAddr')), + ObjectType(ObjectIdentity('IP-MIB', 'ipAddrEntry')), + lookupMib=True, + lexicographicMode=False): + + if errorIndication: + print(errorIndication) + break + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + break + + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py new file mode 100644 index 00000000..22d52589 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py @@ -0,0 +1,44 @@ +""" +Walk Agent, limit number of packets ++++++++++++++++++++++++++++++++++++ + +Send a series of SNMP GETBULK requests using the following options: + +* with SNMPv2c, community name "public" +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for all OIDs past SNMPv2-MIB::system +* with MIB lookup enabled +* run till end-of-mib condition is reported by Agent OR + maxCalls == 10 request-response interactions occur + +Functionally similar to: + +| $ snmpbulkwalk -v2c -c public -Cn0 -Cr50 demo.snmplabs.com SNMPv2-MIB::system + +"""# +from pysnmp.hlapi.v1arch import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in bulkCmd(SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + 0, 50, + ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')), + lookupMib=True, + maxCalls=10): + + if errorIndication: + print(errorIndication) + break + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + break + + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py new file mode 100644 index 00000000..57a1132b --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py @@ -0,0 +1,48 @@ +""" +Sequence Of GET's ++++++++++++++++++ + +Send two SNMP GET requests in a row using the following options: + +* with SNMPv2c, community name "public" +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for IF-MIB::ifInOctets.1 and IF-MIB::ifOutOctets.1 MIB objects +* with MIB lookup enabled + +Use a queue of MIB objects to query. + +The next() call is used to forward Python iterator to the position where it +could consume input + +Functionally similar to: + +| $ snmpget -v2c -c public demo.snmplabs.com IF-MIB::ifInOctets.1 + +"""# +from pysnmp.hlapi.v1arch import * + +queue = [ + [ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1))], + [ObjectType(ObjectIdentity('IF-MIB', 'ifOutOctets', 1))] +] + +iterator = getCmd(SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + lookupMib=True) + +next(iterator) + +while queue: + errorIndication, errorStatus, errorIndex, varBinds = iterator.send(queue.pop()) + if errorIndication: + print(errorIndication) + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py new file mode 100644 index 00000000..39d1e7dd --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py @@ -0,0 +1,41 @@ +""" +Preload PySNMP MIBs ++++++++++++++++++++ + +Send a series of SNMP GETNEXT requests using the following options: + +* with SNMPv2c, community name "public" +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for all OIDs starting from 1.3.6 +* with MIB lookup enabled +* preload all Python MIB modules found in search path + +Functionally similar to: + +| $ snmpwalk -v2c -c public -m ALL demo.snmplabs.com:161 1.3.6 + +"""# +from pysnmp.hlapi.v1arch import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in nextCmd(SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType(ObjectIdentity('1.3.6').loadMibs()), + lookupMib=True): + + if errorIndication: + print(errorIndication) + break + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + break + + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py new file mode 100644 index 00000000..69e943d9 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py @@ -0,0 +1,39 @@ +""" +Walk whole MIB +++++++++++++++ + +Send a series of SNMP GETNEXT requests using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for all OIDs in IF-MIB +* with MIB lookup enabled + +Functionally similar to: + +| $ snmpwalk -v2c -c public demo.snmplabs.com IF-MIB:: + +"""# +from pysnmp.hlapi.v1arch import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in nextCmd(SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType(ObjectIdentity('IF-MIB'))): + + if errorIndication: + print(errorIndication) + break + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + break + + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py new file mode 100644 index 00000000..8d81b1e5 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py @@ -0,0 +1,47 @@ +""" +Fetch whole SNMP table +++++++++++++++++++++++ + +Send a series of SNMP GETNEXT requests using the following options: + +* with SNMPv1, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for some columns of the IF-MIB::ifEntry table +* with MIB lookup enabled +* stop when response OIDs leave the scopes of initial OIDs + +Functionally similar to: + +| $ snmpwalk -v1 -c public demo.snmplabs.com IF-MIB::ifDescr IF-MIB::ifType IF-MIB::ifMtu IF-MIB::ifSpeed IF-MIB::ifPhysAddress IF-MIB::ifType + +"""# +from pysnmp.hlapi.v1arch import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in nextCmd(SnmpDispatcher(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')), + ObjectType(ObjectIdentity('IF-MIB', 'ifType')), + ObjectType(ObjectIdentity('IF-MIB', 'ifMtu')), + ObjectType(ObjectIdentity('IF-MIB', 'ifSpeed')), + ObjectType(ObjectIdentity('IF-MIB', 'ifPhysAddress')), + ObjectType(ObjectIdentity('IF-MIB', 'ifType')), + lookupMib=True, + lexicographicMode=False): + + if errorIndication: + print(errorIndication) + break + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex)-1][0] or '?')) + break + + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py new file mode 100644 index 00000000..29ec78b6 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py @@ -0,0 +1,130 @@ +""" +Query Agents from multiple threads +++++++++++++++++++++++++++++++++++ + +Send a bunch of SNMP GET requests simultaneously using the following options: + +* process 4 GET requests in 3 parallel threads +* with SNMPv1, community 'public' and + with SNMPv2c, community 'public' and +* over IPv4/UDP and + over IPv6/UDP +* to an Agent at demo.snmplabs.com:161 and + to an Agent at demo.snmplabs.com:1161 and + to an Agent at [::1]:161 +* for instances of SNMPv2-MIB::sysDescr.0 and + SNMPv2-MIB::sysLocation.0 MIB objects +* with MIB lookup enabled + +Functionally similar to: + +| $ snmpget -v1 -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 SNMPv2-MIB::sysLocation.0 +| $ snmpget -v2c -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 SNMPv2-MIB::sysLocation.0 +| $ snmpget -v2c -c public demo.snmplabs.com:1161 SNMPv2-MIB::sysDescr.0 SNMPv2-MIB::sysLocation.0 +| $ snmpget -v2c -c public '[::1]' SNMPv2-MIB::sysDescr.0 SNMPv2-MIB::sysLocation.0 + +"""# +from sys import version_info +from threading import Thread +from pysnmp.hlapi.v1arch import * + +if version_info[0] == 2: + from Queue import Queue +else: + from queue import Queue + +# List of targets in the following format: +# ( ( authData, transportTarget, varNames ), ... ) +targets = ( + # 1-st target (SNMPv1 over IPv4/UDP) + (CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # 2-nd target (SNMPv2c over IPv4/UDP) + (CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # 3-nd target (SNMPv2c over IPv4/UDP) - same community and + # different transport address. + (CommunityData('public'), + Udp6TransportTarget(('localhost', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysContact', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)))), + # 4-th target (SNMPv2c over IPv4/UDP) - same community and + # different transport port. + (CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 1161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # N-th target + # ... +) + + +class Worker(Thread): + def __init__(self, requests, responses): + Thread.__init__(self) + self.snmpDispatcher = SnmpDispatcher() + self.requests = requests + self.responses = responses + self.setDaemon(True) + self.start() + + def run(self): + while True: + authData, transportTarget, varBinds = self.requests.get() + + self.responses.append( + next(getCmd(self.snmpDispatcher, + authData, transportTarget, *varBinds, lookupMib=True)) + ) + + if hasattr(self.requests, 'task_done'): # 2.5+ + self.requests.task_done() + + +class ThreadPool(object): + def __init__(self, num_threads): + self.requests = Queue(num_threads) + self.responses = [] + for _ in range(num_threads): + Worker(self.requests, self.responses) + + def addRequest(self, authData, transportTarget, varBinds): + self.requests.put((authData, transportTarget, varBinds)) + + def getResponses(self): + return self.responses + + def waitCompletion(self): + if hasattr(self.requests, 'join'): + self.requests.join() # 2.5+ + else: + from time import sleep + # this is a lame substitute for missing .join() + # adding an explicit synchronization might be a better solution + while not self.requests.empty(): + sleep(1) + + +pool = ThreadPool(3) + +# Submit GET requests +for authData, transportTarget, varBinds in targets: + pool.addRequest(authData, transportTarget, varBinds) + +# Wait for responses or errors +pool.waitCompletion() + +# Walk through responses +for errorIndication, errorStatus, errorIndex, varBinds in pool.getResponses(): + if errorIndication: + print(errorIndication) + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py new file mode 100644 index 00000000..b5498003 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py @@ -0,0 +1,46 @@ +""" +SET scalars values +++++++++++++++++++ + +Send SNMP SET request using the following options: + +* with SNMPv1, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* with MIB lookup enabled +* setting three var-bindings to new values + +Please note, that in this example MIB lookup is only used +for the second var-bindins. For the rest, value types are +inferred from passed objects. + +Functionally similar to: + +| $ snmpset -v1 -c public demo.snmplabs.com 1.3.6.1.2.1.1.9.1.2.1 o 1.3.6.1.4.1.20408.1.1 1.3.6.1.2.1.1.9.1.2.1 = 1.3.6.1.4.1.20408.1.1 1.3.6.1.2.1.1.9.1.3.1 s "new system name" + +"""# +from pysnmp.hlapi.v1arch import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + setCmd(SnmpDispatcher(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysORID', 1), + ObjectIdentifier('1.3.6.1.4.1.20408.1.1')), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.9.1.2.1'), + '1.3.6.1.4.1.20408.1.1'), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.9.1.3.1'), + OctetString('new system name')), + lookupMib=True) +) + +if errorIndication: + print(errorIndication) + +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v1-get.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v1-get.py new file mode 100644 index 00000000..b54802af --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v1-get.py @@ -0,0 +1,36 @@ +""" +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 an instance of SNMPv2-MIB::sysDescr.0 MIB object + * having MIB lookup feature enabled + +Functionally similar to: + +| $ snmpget -v1 -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 + +"""# +from pysnmp.hlapi.v1arch import * + +for response in getCmd(SnmpDispatcher(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + lookupMib=True): + + errorIndication, errorStatus, errorIndex, varBinds = response + + if errorIndication: + print(errorIndication) + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v2c-get.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v2c-get.py new file mode 100644 index 00000000..e2ec5815 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v2c-get.py @@ -0,0 +1,35 @@ +""" +SNMPv2c ++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for two var-bindings with OIDs in string form + +Functionally similar to: + +| $ snmpget -v2c -c public demo.snmplabs.com 1.3.6.1.2.1.1.1.0 1.3.6.1.2.1.1.6.0 + +"""# +from pysnmp.hlapi.v1arch import * + +for response in getCmd(SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ('1.3.6.1.2.1.1.1.0', None), + ('1.3.6.1.2.1.1.6.0', None)): + + errorIndication, errorStatus, errorIndex, varBinds = response + + if errorIndication: + print(errorIndication) + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncio/agent/ntforg/default-v1-trap.py b/examples/hlapi/v3arch/asyncio/agent/ntforg/default-v1-trap.py new file mode 100644 index 00000000..6a0fefda --- /dev/null +++ b/examples/hlapi/v3arch/asyncio/agent/ntforg/default-v1-trap.py @@ -0,0 +1,52 @@ +""" +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" + +"""# +import asyncio +from pysnmp.hlapi.v3arch.asyncio import * + + +@asyncio.coroutine +def run(): + snmpEngine = SnmpEngine() + errorIndication, errorStatus, errorIndex, varBinds = yield from sendNotification( + snmpEngine, + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 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')) + ) + ) + + if errorIndication: + print(errorIndication) + + snmpEngine.transportDispatcher.closeDispatcher() + + +asyncio.get_event_loop().run_until_complete(run()) diff --git a/examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-notifications-at-once.py b/examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-notifications-at-once.py new file mode 100644 index 00000000..f1cae2b8 --- /dev/null +++ b/examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-notifications-at-once.py @@ -0,0 +1,66 @@ +""" +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 + +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 +| $ snmptrap -v2c -c public demo.snmplabs.com 12345 1.3.6.1.6.3.1.1.5.2 + +"""# +import asyncio +from pysnmp.hlapi.v3arch.asyncio import * + + +@asyncio.coroutine +def sendone(snmpEngine, hostname, notifyType): + (errorIndication, + errorStatus, + errorIndex, + varBinds) = yield from 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')) + ) + ) + + if errorIndication: + print(errorIndication) + elif errorStatus: + print('%s: at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) + + +snmpEngine = SnmpEngine() + +loop = asyncio.get_event_loop() +loop.run_until_complete( + asyncio.wait([sendone(snmpEngine, 'demo.snmplabs.com', 'trap'), + sendone(snmpEngine, 'demo.snmplabs.com', 'inform')]) +) diff --git a/examples/hlapi/v3arch/asyncio/manager/cmdgen/getbulk-to-eom.py b/examples/hlapi/v3arch/asyncio/manager/cmdgen/getbulk-to-eom.py new file mode 100644 index 00000000..c60d6e12 --- /dev/null +++ b/examples/hlapi/v3arch/asyncio/manager/cmdgen/getbulk-to-eom.py @@ -0,0 +1,63 @@ +""" +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 asyncio I/O framework + +Functionally similar to: + +| $ snmpbulkwalk -v3 -lnoAuthNoPriv -u usr-none-none -Cn0 -Cr50 \ +| demo.snmplabs.com SNMPv2-MIB::system + +"""# +import asyncio +from pysnmp.hlapi.v3arch.asyncio import * + + +@asyncio.coroutine +def run(varBinds): + snmpEngine = SnmpEngine() + while True: + (errorIndication, + errorStatus, + errorIndex, + varBindTable) = yield from bulkCmd( + snmpEngine, + UsmUserData('usr-none-none'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + 0, 50, + *varBinds) + + if errorIndication: + print(errorIndication) + break + elif errorStatus: + print('%s at %s' % ( + errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?' + ) + ) + else: + for varBindRow in varBindTable: + for varBind in varBindRow: + print(' = '.join([x.prettyPrint() for x in varBind])) + + varBinds = varBindTable[-1] + if isEndOfMib(varBinds): + break + + snmpEngine.transportDispatcher.closeDispatcher() + + +loop = asyncio.get_event_loop() +loop.run_until_complete( + run([ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr'))]) +) diff --git a/examples/hlapi/v3arch/asyncio/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py b/examples/hlapi/v3arch/asyncio/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py new file mode 100644 index 00000000..a1b5d9a0 --- /dev/null +++ b/examples/hlapi/v3arch/asyncio/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py @@ -0,0 +1,54 @@ +""" +Concurrent queries +++++++++++++++++++ + +Send multiple SNMP GET requests at once using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to multiple Agents at demo.snmplabs.com +* for instance of SNMPv2-MIB::sysDescr.0 MIB object +* based on asyncio I/O framework + +Functionally similar to: + +| $ snmpget -v2c -c public demo.snmplabs.com:1161 SNMPv2-MIB::sysDescr.0 +| $ snmpget -v2c -c public demo.snmplabs.com:2161 SNMPv2-MIB::sysDescr.0 +| $ snmpget -v2c -c public demo.snmplabs.com:3161 SNMPv2-MIB::sysDescr.0 + +"""# +import asyncio +from pysnmp.hlapi.v3arch.asyncio import * + + +@asyncio.coroutine +def getone(snmpEngine, hostname): + errorIndication, errorStatus, errorIndex, varBinds = yield from getCmd( + snmpEngine, + CommunityData('public'), + UdpTransportTarget(hostname), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)) + ) + + if errorIndication: + print(errorIndication) + elif errorStatus: + print('%s at %s' % ( + errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?' + ) + ) + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) + + +snmpEngine = SnmpEngine() + +loop = asyncio.get_event_loop() +loop.run_until_complete( + asyncio.wait([getone(snmpEngine, ('demo.snmplabs.com', 1161)), + getone(snmpEngine, ('demo.snmplabs.com', 2161)), + getone(snmpEngine, ('demo.snmplabs.com', 3161))]) +) diff --git a/examples/hlapi/v3arch/asyncio/manager/cmdgen/multiple-sequential-queries.py b/examples/hlapi/v3arch/asyncio/manager/cmdgen/multiple-sequential-queries.py new file mode 100644 index 00000000..d546870d --- /dev/null +++ b/examples/hlapi/v3arch/asyncio/manager/cmdgen/multiple-sequential-queries.py @@ -0,0 +1,58 @@ +""" +Sequential queries +++++++++++++++++++ + +Send multiple SNMP GET requests one by one using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to multiple Agents at demo.snmplabs.com +* for instance of SNMPv2-MIB::sysDescr.0 MIB object +* based on asyncio I/O framework + +Functionally similar to: + +| $ snmpget -v2c -c public demo.snmplabs.com:1161 SNMPv2-MIB::sysDescr.0 +| $ snmpget -v2c -c public demo.snmplabs.com:2161 SNMPv2-MIB::sysDescr.0 +| $ snmpget -v2c -c public demo.snmplabs.com:3161 SNMPv2-MIB::sysDescr.0 + +"""# +import asyncio +from pysnmp.hlapi.v3arch.asyncio import * + + +@asyncio.coroutine +def getone(snmpEngine, hostname): + errorIndication, errorStatus, errorIndex, varBinds = yield from getCmd( + snmpEngine, + CommunityData('public'), + UdpTransportTarget(hostname), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)) + ) + + if errorIndication: + print(errorIndication) + elif errorStatus: + print('%s at %s' % ( + errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?' + ) + ) + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) + + +@asyncio.coroutine +def getall(snmpEngine, hostnames): + for hostname in hostnames: + yield from getone(snmpEngine, hostname) + + +snmpEngine = SnmpEngine() + +loop = asyncio.get_event_loop() +loop.run_until_complete(getall(snmpEngine, [('demo.snmplabs.com', 1161), + ('demo.snmplabs.com', 2161), + ('demo.snmplabs.com', 3161)])) diff --git a/examples/hlapi/v3arch/asyncio/manager/cmdgen/v1-get.py b/examples/hlapi/v3arch/asyncio/manager/cmdgen/v1-get.py new file mode 100644 index 00000000..f14a9f63 --- /dev/null +++ b/examples/hlapi/v3arch/asyncio/manager/cmdgen/v1-get.py @@ -0,0 +1,48 @@ +""" +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 an instance of SNMPv2-MIB::sysDescr.0 MIB object + * Based on asyncio I/O framework + +Functionally similar to: + +| $ snmpget -v1 -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 + +"""# +import asyncio +from pysnmp.hlapi.v3arch.asyncio import * + + +@asyncio.coroutine +def run(): + snmpEngine = SnmpEngine() + errorIndication, errorStatus, errorIndex, varBinds = yield from getCmd( + snmpEngine, + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)) + ) + + if errorIndication: + print(errorIndication) + elif errorStatus: + print('%s at %s' % ( + errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?' + ) + ) + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) + + snmpEngine.transportDispatcher.closeDispatcher() + + +asyncio.get_event_loop().run_until_complete(run()) diff --git a/examples/hlapi/v3arch/asyncore/agent/ntforg/default-v1-trap.py b/examples/hlapi/v3arch/asyncore/agent/ntforg/default-v1-trap.py new file mode 100644 index 00000000..21ac77b3 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/agent/ntforg/default-v1-trap.py @@ -0,0 +1,43 @@ +""" +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 pysnmp.hlapi.v3arch.asyncore import * + +snmpEngine = SnmpEngine() + +sendNotification( + snmpEngine, + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 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')) + ).loadMibs( + 'SNMPv2-MIB' + ) +) + +snmpEngine.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v3arch/asyncore/agent/ntforg/multiple-informs-at-once.py b/examples/hlapi/v3arch/asyncore/agent/ntforg/multiple-informs-at-once.py new file mode 100644 index 00000000..9690c2e4 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/agent/ntforg/multiple-informs-at-once.py @@ -0,0 +1,64 @@ +""" +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 + +| $ snmpinform -v2c -c public demo.snmplabs.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.snmplabs.com 123 1.3.6.1.6.3.1.1.5.1 + +"""# +from pysnmp.hlapi.v3arch.asyncore import * + +# List of targets in the followin format: +# ( ( authData, transportTarget ), ... ) +targets = ( + # 1-st target (SNMPv2c over IPv4/UDP) + (CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData()), + # 2-nd target (SNMPv3 over IPv4/UDP) + (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData()), +) + + +# noinspection PyUnusedLocal +def cbFun(snmpEngine, sendRequestHandle, errorIndication, + errorStatus, errorIndex, varBinds, cbCtx): + if errorIndication: + print('Notification %s not sent: %s' % (sendRequestHandle, errorIndication)) + elif errorStatus: + print('Notification Receiver returned error for %s: %s @%s' % + (sendRequestHandle, errorStatus, errorIndex)) + else: + print('Notification %s delivered:' % sendRequestHandle) + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + + +snmpEngine = SnmpEngine() + +for authData, transportTarget, contextData in targets: + sendPduHandle = sendNotification( + snmpEngine, + authData, + transportTarget, + contextData, + 'inform', # NotifyType + NotificationType( + ObjectIdentity('SNMPv2-MIB', 'coldStart') + ).addVarBinds(('1.3.6.1.2.1.1.1.0', 'my name')), + cbFun=cbFun + ) + +snmpEngine.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v3arch/asyncore/agent/ntforg/multiple-traps-at-once.py b/examples/hlapi/v3arch/asyncore/agent/ntforg/multiple-traps-at-once.py new file mode 100644 index 00000000..ad873868 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/agent/ntforg/multiple-traps-at-once.py @@ -0,0 +1,52 @@ +""" +Multiple concurrent queries ++++++++++++++++++++++++++++ + +Send a bunch of different SNMP Notifications to different peers all at once, +wait for responses asynchronously: + +* SNMPv1 and SNMPv2c +* 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 + +| $ snmptrap -v1 -c public demo.snmplabs.com 1.3.6.1.4.1.20408.4.1.1.2 demo.snmplabs.com 6 432 12345 1.3.6.1.2.1.1.1.0 s "my system" +| $ snmptrap -v2c -c public demo.snmplabs.com 123 1.3.6.1.6.3.1.1.5.1 + +"""# +from pysnmp.hlapi.v3arch.asyncore import * + +# List of targets in the followin format: +# ( ( authData, transportTarget ), ... ) +targets = ( + # 1-st target (SNMPv1 over IPv4/UDP) + (CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData()), + # 2-nd target (SNMPv2c over IPv4/UDP) + (CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData()), +) + +snmpEngine = SnmpEngine() + +for authData, transportTarget, contextData in targets: + sendNotification( + snmpEngine, + authData, + transportTarget, + contextData, + 'trap', # NotifyType + NotificationType( + ObjectIdentity('SNMPv2-MIB', 'coldStart') + ).addVarBinds( + (ObjectIdentifier('1.3.6.1.2.1.1.1.0'), + OctetString('my name')) + ) + ) + +snmpEngine.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v3arch/asyncore/agent/ntforg/running-multiple-snmp-engines-at-once.py b/examples/hlapi/v3arch/asyncore/agent/ntforg/running-multiple-snmp-engines-at-once.py new file mode 100644 index 00000000..10a96756 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/agent/ntforg/running-multiple-snmp-engines-at-once.py @@ -0,0 +1,92 @@ +""" +Multiple SNMP Engines ++++++++++++++++++++++ + +Send SNMP notifications in behalf of multiple independend 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 IMFORM 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 +criterias are also possible. + +| $ snmpinform -v2c -c public demo.snmplabs.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.snmplabs.com 123 1.3.6.1.6.3.1.1.5.1 + +"""# +from pysnmp.hlapi.v3arch.asyncore import * +from pysnmp.carrier.asyncore.dispatch import AsyncoreDispatcher + +# List of targets in the following format: +# ( ( authData, transportTarget ), ... ) +targets = ( + # 1-st target (SNMPv2c over IPv4/UDP) + (CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 1162)), + ContextData()), + # 2-nd target (SNMPv3 over IPv4/UDP) + (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData()), +) + + +# noinspection PyUnusedLocal +def cbFun(snmpEngine, sendRequestHandle, errorIndication, + errorStatus, errorIndex, varBinds, cbCtx): + snmpEngine = cbCtx + if errorIndication: + print('Notification %s for %s not sent: %s' % (sendRequestHandle, snmpEngine.snmpEngineID.prettyPrint(), errorIndication)) + elif errorStatus: + print('Notification Receiver returned error for request %s, SNMP Engine %s: %s @%s' % (sendRequestHandle, snmpEngine.snmpEngineID.prettyPrint(), errorStatus, errorIndex)) + else: + print('Notification %s for SNMP Engine %s delivered:' % (sendRequestHandle, snmpEngine.snmpEngineID.prettyPrint())) + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + + +# Instantiate the single transport dispatcher object +transportDispatcher = AsyncoreDispatcher() + +# 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: + snmpEngine = (transportTarget.getTransportInfo()[1][1] % 3 and + snmpEngineA or snmpEngineB) + sendPduHandle = sendNotification( + snmpEngine, + authData, + transportTarget, + contextData, + 'inform', # NotifyType + NotificationType( + ObjectIdentity('SNMPv2-MIB', 'coldStart') + ).addVarBinds(('1.3.6.1.2.1.1.1.0', 'my name')), + cbFun=cbFun, cbCtx=snmpEngine + ) + +transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v3arch/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py b/examples/hlapi/v3arch/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py new file mode 100644 index 00000000..c6132711 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py @@ -0,0 +1,80 @@ +""" +Multiple concurrent queries ++++++++++++++++++++++++++++ + +Send a bunch of different SNMP GET requests to different peers all at once, +wait for responses asynchronously: + +* with SNMPv1, community 'public' and + with SNMPv2c, community 'public' and + with SNMPv3, user 'usr-md5-des', MD5 auth and DES privacy +* over IPv4/UDP and + over IPv6/UDP +* to an Agent at demo.snmplabs.com:161 and + to an Agent at [::1]:161 +* for instances of SNMPv2-MIB::sysDescr.0 and + SNMPv2-MIB::sysLocation.0 MIB objects + +"""# +from pysnmp.hlapi.v3arch.asyncore import * + +# List of targets in the followin format: +# ( ( authData, transportTarget, varNames ), ... ) +targets = ( + # 1-st target (SNMPv1 over IPv4/UDP) + (CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # 2-nd target (SNMPv2c over IPv4/UDP) + (CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # 3-nd target (SNMPv2c over IPv4/UDP) - same community and + # different transport address. + (CommunityData('public'), + UdpTransportTarget(('localhost', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysContact', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)))), + # 4-nd target (SNMPv3 over IPv4/UDP) + (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # 5-th target (SNMPv3 over IPv6/UDP) + (UsmUserData('usr-md5-none', 'authkey1'), + Udp6TransportTarget(('::1', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # N-th target + # ... +) + + +# Wait for responses or errors +# noinspection PyUnusedLocal,PyUnusedLocal +def cbFun(snmpEngine, sendRequestHandle, errorIndication, + errorStatus, errorIndex, varBinds, cbCtx): + authData, transportTarget = cbCtx + print('%s via %s' % (authData, transportTarget)) + if errorIndication: + print(errorIndication) + return True + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + return True + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) + + +snmpEngine = SnmpEngine() + +# Submit GET requests +for authData, transportTarget, varNames in targets: + getCmd(snmpEngine, authData, transportTarget, ContextData(), *varNames, + **dict(cbFun=cbFun, cbCtx=(authData, transportTarget))) + +snmpEngine.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v3arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py b/examples/hlapi/v3arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py new file mode 100644 index 00000000..eef035d1 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py @@ -0,0 +1,73 @@ +""" +Walk multiple Agents at once +++++++++++++++++++++++++++++ + +Iterate over MIBs of multiple SNMP Agents asynchronously using the +following options: + +* with SNMPv1, community 'public' and + with SNMPv2c, community 'public' and + with SNMPv3, user 'usr-md5-des', MD5 auth and DES privacy +* over IPv4/UDP and + over IPv6/UDP +* to an Agent at demo.snmplabs.com:161 and + to an Agent at [::1]:161 +* pull variables till EOM + +"""# +from pysnmp.hlapi.v3arch.asyncore import * + +# List of targets in the following format: +# ((authData, transportTarget, varNames), ...) +targets = ( + # 1-st target (SNMPv1 over IPv4/UDP) + (CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + (ObjectType(ObjectIdentity('1.3.6.1.2.1')), + ObjectType(ObjectIdentity('1.3.6.1.3.1')))), + # 2-nd target (SNMPv2c over IPv4/UDP) + (CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + (ObjectType(ObjectIdentity('1.3.6.1.4.1')),)), + # 3-nd target (SNMPv3 over IPv4/UDP) + (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')),)), + # 4-th target (SNMPv3 over IPv6/UDP) + (UsmUserData('usr-md5-none', 'authkey1'), + Udp6TransportTarget(('::1', 161)), + (ObjectType(ObjectIdentity('IF-MIB', 'ifTable')),)) + # N-th target + # ... +) + + +# Wait for responses or errors, submit GETNEXT requests for further OIDs +# noinspection PyUnusedLocal,PyUnusedLocal +def cbFun(snmpEngine, sendRequestHandle, errorIndication, + errorStatus, errorIndex, varBindTable, cbCtx): + (authData, transportTarget) = cbCtx + print('%s via %s' % (authData, transportTarget)) + if errorIndication: + print(errorIndication) + return + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBindTable[-1][int(errorIndex) - 1][0] or '?')) + return + else: + for varBindRow in varBindTable: + for varBind in varBindRow: + print(' = '.join([x.prettyPrint() for x in varBind])) + + return True # continue table retrieval + + +snmpEngine = SnmpEngine() + +# Submit initial GETNEXT requests and wait for responses +for authData, transportTarget, varBinds in targets: + nextCmd(snmpEngine, authData, transportTarget, ContextData(), + *varBinds, **dict(cbFun=cbFun, cbCtx=(authData, transportTarget))) + +snmpEngine.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v3arch/asyncore/manager/cmdgen/pull-whole-mib.py b/examples/hlapi/v3arch/asyncore/manager/cmdgen/pull-whole-mib.py new file mode 100644 index 00000000..a0aa02b9 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/manager/cmdgen/pull-whole-mib.py @@ -0,0 +1,49 @@ +""" +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 + +Functionally similar to: + +| $ snmpwalk -v3 -lauthNoPriv -u usr-md5-none -A authkey1 -X privkey1 \ +| demo.snmplabs.com IF-MIB:: + +"""# +from pysnmp.hlapi.v3arch.asyncore import * + + +# noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal +def cbFun(snmpEngine, sendRequestHandle, errorIndication, + errorStatus, errorIndex, varBindTable, cbCtx): + if errorIndication: + print(errorIndication) + return + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBindTable[-1][int(errorIndex) - 1][0] or '?')) + return + else: + for varBindRow in varBindTable: + for varBind in varBindRow: + print(' = '.join([x.prettyPrint() for x in varBind])) + + return True # request lower layers to do GETNEXT and call us back + + +snmpEngine = SnmpEngine() + +nextCmd(snmpEngine, + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')), + ObjectType(ObjectIdentity('IF-MIB', 'ifTable')), + cbFun=cbFun) + +snmpEngine.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v3arch/asyncore/manager/cmdgen/query-multiple-snmp-engines-over-ipv4-and-ipv6.py b/examples/hlapi/v3arch/asyncore/manager/cmdgen/query-multiple-snmp-engines-over-ipv4-and-ipv6.py new file mode 100644 index 00000000..87591b4e --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/manager/cmdgen/query-multiple-snmp-engines-over-ipv4-and-ipv6.py @@ -0,0 +1,95 @@ +""" +Multiple SNMP engines ++++++++++++++++++++++ + +Send multiple SNMP GET requests to multiple peers using multiple +independent SNMP engines. Deal with peers asynchronously. SNMP options +are: + +* with SNMPv1, community 'public' and + with SNMPv2c, community 'public' and + with SNMPv3, user 'usr-md5-des', MD5 auth and DES privacy +* over IPv4/UDP and + over IPv6/UDP +* to an Agent at demo.snmplabs.com:161 and + to an Agent at [::1]:161 +* for instances of SNMPv2-MIB::sysDescr.0 and + SNMPv2-MIB::sysLocation.0 MIB objects + +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 AsyncCommandGenerator with each of +SNMP Engines to comunicate GET command request 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 +criterias are also possible. + +"""# +from pysnmp.hlapi.v3arch.asyncore import * +from pysnmp.carrier.asyncore.dispatch import AsyncoreDispatcher + +# List of targets in the following format: +# ( ( authData, transportTarget, varNames ), ... ) +targets = ( + # 1-st target (SNMPv1 over IPv4/UDP) + (CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # 2-nd target (SNMPv2c over IPv4/UDP) + (CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 1161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # 3-nd target (SNMPv3 over IPv4/UDP) + (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + UdpTransportTarget(('demo.snmplabs.com', 2161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))) + # N-th target + # ... +) + + +# Wait for responses or errors +# noinspection PyUnusedLocal,PyUnusedLocal +def cbFun(snmpEngine, sendRequestHandle, errorIndication, + errorStatus, errorIndex, varBinds, cbCtx): + (snmpEngine, authData, transportTarget) = cbCtx + print('snmpEngine %s: %s via %s' % (snmpEngine.snmpEngineID.prettyPrint(), authData, transportTarget)) + if errorIndication: + print(errorIndication) + return True + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + return True + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) + + +# Instantiate the single transport dispatcher object +transportDispatcher = AsyncoreDispatcher() + +# 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, varBinds in targets: + snmpEngine = transportTarget.getTransportInfo()[1][1] % 3 and \ + snmpEngineA or snmpEngineB + getCmd(snmpEngine, authData, transportTarget, ContextData(), *varBinds, + **dict(cbFun=cbFun, cbCtx=(snmpEngine, authData, transportTarget))) + +transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v3arch/asyncore/manager/cmdgen/v2c-get.py b/examples/hlapi/v3arch/asyncore/manager/cmdgen/v2c-get.py new file mode 100644 index 00000000..ae8f8262 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/manager/cmdgen/v2c-get.py @@ -0,0 +1,44 @@ +""" +SNMPv2c ++++++++ + +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, + +Functionally similar to: + +| $ snmpget -v2c -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 + +"""# +from pysnmp.hlapi.v3arch.asyncore import * + + +# noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal +def cbFun(snmpEngine, sendRequestHandle, errorIndication, + errorStatus, errorIndex, varBinds, cbCtx): + if errorIndication: + print(errorIndication) + return + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBindTable[-1][int(errorIndex) - 1][0] or '?')) + return + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) + + +snmpEngine = SnmpEngine() + +getCmd(snmpEngine, + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + cbFun=cbFun) + +snmpEngine.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-contextengineid.py b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-contextengineid.py new file mode 100644 index 00000000..1b9e12d5 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-contextengineid.py @@ -0,0 +1,44 @@ +""" +INFORM with custom ContextEngineId +++++++++++++++++++++++++++++++++++ + +Send SNMP notification using the following options: + +* SNMPv3 +* with user 'usr-md5-none', MD5 auth, no priv +* send INFORM notification +* in behalf of contextEngineId 0x8000000004030201, contextName '' +* over IPv4/UDP +* with TRAP ID 'warmStart' specified as a string OID + +Sending SNMPv3 Notification in behalf of non-default ContextEngineId +requires having a collection of Managed Objects registered under +the ContextEngineId being used. + +Functionally similar to: + +| $ snmpinform -v3 -l authNoPriv -u usr-md5-none -A authkey1 -E 0x8000000004030201 demo.snmplabs.com 12345 1.3.6.1.6.3.1.1.5.2 + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + sendNotification(SnmpEngine(), + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData(OctetString(hexValue='8000000004030201')), + 'inform', + NotificationType( + ObjectIdentity('1.3.6.1.6.3.1.1.5.2') + ).loadMibs('SNMPv2-MIB') + ) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-contextname.py b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-contextname.py new file mode 100644 index 00000000..96abba48 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-contextname.py @@ -0,0 +1,41 @@ +""" +INFORM with custom ContextName +++++++++++++++++++++++++++++++ + +Send SNMP notification using the following options: + +* SNMPv3 +* with user 'usr-md5-none', MD5 auth, no priv +* send INFORM notification +* in behalf of contextEngineId = SnmpEngineId, contextName 'my-context' +* over IPv4/UDP +* with TRAP ID 'warmStart' specified as a string OID + +Sending SNMPv3 Notification in behalf of non-default ContextName +requires having a collection of Managed Objects registered under +the ContextName being used. + +Functionally similar to: + +| $ snmpinform -v3 -l authNoPriv -u usr-md5-none -A authkey1 -n my-context demo.snmplabs.com 12345 0.3.6.1.6.3.1.1.5.2 + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + sendNotification(SnmpEngine(), + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData(contextName='my-context'), + 'inform', + NotificationType(ObjectIdentity('1.3.6.1.6.3.1.1.5.2')).loadMibs('SNMPv2-MIB')) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-v1-trap.py b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-v1-trap.py new file mode 100644 index 00000000..c6d84303 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-v1-trap.py @@ -0,0 +1,47 @@ +""" +Custom SNMPv1 TRAP +++++++++++++++++++ + +Send SNMPv1 TRAP through unified SNMPv3 message processing framework. + +Original v1 TRAP fields are mapped into dedicated variable-bindings, +(see `RFC2576 `_) for details. + +* SNMPv1 +* with community name 'public' +* over IPv4/UDP +* send TRAP notification +* with Generic Trap #6 (enterpriseSpecific) and Specific Trap 432 +* overriding Uptime value with 12345 +* overriding Agent Address with '127.0.0.1' +* overriding Enterprise OID with 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 127.0.0.1 6 432 12345 1.3.6.1.2.1.1.1.0 s "my system" + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + sendNotification( + SnmpEngine(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData(), + 'trap', + NotificationType( + ObjectIdentity('1.3.6.1.4.1.20408.4.1.1.2.0.432'), + ).addVarBinds( + ('1.3.6.1.2.1.1.3.0', 12345), + ('1.3.6.1.6.3.18.1.3.0', '127.0.0.1'), + ('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')) + ).loadMibs( + 'SNMPv2-MIB', 'SNMP-COMMUNITY-MIB' + ) + ) +) +if errorIndication: + print(errorIndication) diff --git a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/default-v1-trap.py b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/default-v1-trap.py new file mode 100644 index 00000000..552ad877 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/default-v1-trap.py @@ -0,0 +1,44 @@ +""" +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 pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + sendNotification( + SnmpEngine(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 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')) + ).loadMibs( + 'SNMPv2-MIB' + ) + ) +) + +if errorIndication: + print(errorIndication) diff --git a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/send-notification-with-additional-varbinds.py b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/send-notification-with-additional-varbinds.py new file mode 100644 index 00000000..46f3c5c1 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/send-notification-with-additional-varbinds.py @@ -0,0 +1,43 @@ +""" +Sending additional var-binds +++++++++++++++++++++++++++++ + +Send SNMP notification using the following options: + +* SNMPv2c +* with community name 'public' +* over IPv4/UDP +* send INFORM notification +* with TRAP ID 'coldStart' specified as a MIB symbol +* include managed object information specified as a MIB symbol + +Functionally similar to: + +| $ snmpinform -v2c -c public demo.snmplabs.com 12345 1.3.6.1.6.3.1.1.5.1 1.3.6.1.2.1.1.1.0 s "my system" + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + sendNotification( + SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData(), + 'inform', + NotificationType( + ObjectIdentity('SNMPv2-MIB', 'coldStart') + ).addVarBinds( + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0), 'my system') + ) + ) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py new file mode 100644 index 00000000..5681d28c --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py @@ -0,0 +1,41 @@ +""" +SNMPv2c TRAP via NOTIFICATION-TYPE +++++++++++++++++++++++++++++++++++ + +Initialize TRAP message contents from variables specified +in *NOTIFICATION-TYPE* SMI macro. + +* SNMPv2c +* with community name 'public' +* over IPv4/UDP +* send TRAP notification +* with TRAP ID 'linkUp' specified as a MIB symbol +* include values for managed objects implicitly added to notification + (via NOTIFICATION-TYPE->OBJECTS) + +Functionally similar to: + +| $ snmptrap -v2c -c public demo.snmplabs.com 0 1.3.6.1.6.3.1.1.5.1 1.3.6.1.2.1.2.2.1.1.123 i 123 1.3.6.1.2.1.2.2.1.7.123 i 1 1.3.6.1.2.1.2.2.1.8.123 i 1 + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + sendNotification( + SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData(), + 'trap', + NotificationType( + ObjectIdentity('IF-MIB', 'linkUp'), + instanceIndex=(123,), + objects={('IF-MIB', 'ifIndex'): 123, + ('IF-MIB', 'ifAdminStatus'): 'up', + ('IF-MIB', 'ifOperStatus'): 'up'} + ) + ) +) + +if errorIndication: + print(errorIndication) diff --git a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v3-inform.py b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v3-inform.py new file mode 100644 index 00000000..6f02024f --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v3-inform.py @@ -0,0 +1,45 @@ +""" +INFORM, auth: MD5 privacy: DES +++++++++++++++++++++++++++++++ + +Send SNMP INFORM notification using the following options: + +* SNMPv3 +* with user 'usr-md5-des', auth: MD5, priv DES +* over IPv4/UDP +* send INFORM notification +* with TRAP ID 'warmStart' specified as a string OID +* include managed object information 1.3.6.1.2.1.1.5.0 = 'system name' + +Functionally similar to: + +| $ snmpinform -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 demo.snmplabs.com 12345 1.3.6.1.4.1.20408.4.1.1.2 1.3.6.1.2.1.1.1.0 s "my system" + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + sendNotification( + SnmpEngine(), + UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData(), + 'inform', + NotificationType( + ObjectIdentity('1.3.6.1.6.3.1.1.5.2') + ).addVarBinds( + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.5.0'), 'system name') + ).loadMibs( + 'SNMPv2-MIB' + ) + ) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v3-trap.py b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v3-trap.py new file mode 100644 index 00000000..836e8d50 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v3-trap.py @@ -0,0 +1,41 @@ +""" +SNMPv3 TRAP: auth SHA, privacy: AES128 +++++++++++++++++++++++++++++++++++++++ + +Send SNMP notification using the following options: + +* SNMPv3 +* with authoritative snmpEngineId = 0x8000000001020304 + (USM must be configured at the Receiver accordingly) +* with user 'usr-sha-aes128', auth: SHA, priv: AES128 +* over IPv4/UDP +* send TRAP notification +* with TRAP ID 'authenticationFailure' specified as a MIB symbol +* do not include any additional managed object information + +SNMPv3 TRAPs requires pre-sharing the Notification Originator's +value of SnmpEngineId with Notification Receiver. To facilitate that +we will use static (e.g. not autogenerated) version of snmpEngineId. + +Functionally similar to: + +| $ snmptrap -v3 -e 8000000001020304 -l authPriv -u usr-sha-aes -A authkey1 -X privkey1 -a SHA -x AES demo.snmplabs.com 12345 1.3.6.1.4.1.20408.4.1.1.2 1.3.6.1.2.1.1.1.0 s "my system" + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + sendNotification( + SnmpEngine(OctetString(hexValue='8000000001020304')), + UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', + authProtocol=usmHMACSHAAuthProtocol, + privProtocol=usmAesCfb128Protocol), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData(), + 'trap', + NotificationType(ObjectIdentity('SNMPv2-MIB', 'authenticationFailure')) + ) +) + +if errorIndication: + print(errorIndication) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py new file mode 100644 index 00000000..b6fb879e --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py @@ -0,0 +1,35 @@ +""" +Coerce value to SET to MIB spec ++++++++++++++++++++++++++++++++ + +Send SNMP SET request using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* setting SNMPv2-MIB::sysName.0 to new value (type taken from MIB) + +Functionally similar to: + +| $ snmpset -v2c -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 = "new system name" + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + setCmd(SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysORDescr', 1), + 'new system name')) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py new file mode 100644 index 00000000..f25fafe4 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py @@ -0,0 +1,36 @@ +""" +Custom ASN.1 MIB path ++++++++++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for IF-MIB::ifInOctets.1 MIB object +* pass non-default ASN.1 MIB source to MIB compiler + +Functionally similar to: + +| $ snmpget -v2c -c public -M /usr/share/snmp demo.snmplabs.com IF-MIB::ifInOctets.1 + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1).addAsn1MibSource('file:///usr/share/snmp', + 'http://mibs.snmplabs.com/asn1/@mib@'))) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-contextengineid-and-contextname.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-contextengineid-and-contextname.py new file mode 100644 index 00000000..497af602 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-contextengineid-and-contextname.py @@ -0,0 +1,38 @@ +""" +Custom ContextEngineId and ContextName +++++++++++++++++++++++++++++++++++++++ + +Send SNMP SET request using the following options: + +* with SNMPv3 with user 'usr-md5-none', MD5 auth and no privacy protocols +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* addressing particular set of Managed Objects at remote SNMP Engine by: + * contextEngineId 0x80004fb805636c6f75644dab22cc and + * contextName 'a172334d7d97871b72241397f713fa12' +* setting SNMPv2-MIB::sysName.0 to new value (type taken from MIB) + +Functionally similar to: + +| $ snmpset -v3 -u usr-md5-des -l authPriv -A authkey1 -X privkey1 -E 80004fb805636c6f75644dab22cc -n a172334d7d97871b72241397f713fa12 demo.snmplabs.com SNMPv2-MIB::sysORDescr.1 = "new system name" + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + setCmd(SnmpEngine(), + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(contextEngineId=OctetString(hexValue='80004fb805636c6f75644dab22cc'), + contextName='da761cfc8c94d3aceef4f60f049105ba'), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysORDescr', 1), 'new system name')) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-contextengineid.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-contextengineid.py new file mode 100644 index 00000000..6435a5d5 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-contextengineid.py @@ -0,0 +1,37 @@ +""" +Custom ContextEngineId +++++++++++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols +* use remote SNMP Engine ID 0x80004fb805636c6f75644dab22cc (USM + autodiscovery will run) +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* setting SNMPv2-MIB::sysName.0 to new value (type coerced from MIB) + +Functionally similar to: + +| $ snmpset -v3 -u usr-md5-des -l authPriv -A authkey1 -X privkey1 -E 80004fb805636c6f75644dab22cc demo.snmplabs.com SNMPv2-MIB::sysORDescr.1 = "new system name" + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + setCmd(SnmpEngine(), + UsmUserData('usr-md5-des', 'authkey1', 'privkey1', + securityEngineId=OctetString(hexValue='80004fb805636c6f75644dab22cc')), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0), 'new system name')) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py new file mode 100644 index 00000000..26b8954a --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py @@ -0,0 +1,44 @@ +""" +Custom PySNMP MIBs location ++++++++++++++++++++++++++++ + +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 within TCP-MIB::tcpConnTable column +* TCP-MIB Python module will be searched by a user-specified filesystem + path (/opt/mib/pysnmp) and in Python package (python_packaged_mibs) + which should be in sys.path + +Functionally similar to: + +| $ snmpbulkwalk -v3 -lnoAuthNoPriv -u usr-none-none -Cn0 -Cr50 demo.snmplabs.com TCP-MIB::tcpConnTable + +"""# +from pysnmp.hlapi import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in bulkCmd(SnmpEngine(), + UsmUserData('usr-none-none'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + 0, 50, + ObjectType( + ObjectIdentity('TCP-MIB', 'tcpConnTable').addMibSource('/opt/mibs/pysnmp').addMibSource('python_packaged_mibs') + ), + lexicographicMode=False): + + if errorIndication: + print(errorIndication) + break + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + break + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py new file mode 100644 index 00000000..d70a322f --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py @@ -0,0 +1,50 @@ +""" +Custom request timeout +++++++++++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for an OID in string form +* use custom timeout and request retries values + +Transport timing settings (maximum number of request retries and +individual request timeout in seconds) can be set on a per-target basis +as explained by the code that follows. + +Keep in mind that while timeout value can be specified in fractions of a +second, default pysnmp timer resolution is quite low (about 0.5 sec) +so there's no much point in using timeouts which is not a multiple of 0.5 +Internal timer can be programmatically adjusted to finer resolution if needed. + +If retries value is set to 0, pysnmp will issue a single request. Even +if no response arrives, there will be no retry. Likewise, retries=1 +means one initial request plus one retry. + +Functionally similar to: + +| $ snmpget -v1 -c public -t 2 -r 0 demo.snmplabs.com 1.3.6.1.2.1.1.1.0 + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpEngine(), + CommunityData('public'), + UdpTransportTarget( + ('demo.snmplabs.com', 161), timeout=2.0, retries=0 + ), + ContextData(), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'))) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-v3-security-name.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-v3-security-name.py new file mode 100644 index 00000000..8e541f7b --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-v3-security-name.py @@ -0,0 +1,35 @@ +""" +Custom SecurityName ++++++++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv3, user 'usr-md5-none', securityName 'myuser' + MD5 authentication, no privacy +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for an OID in text form + +The securityName parameter can be thought as an alias to userName and +allows you to address a USM Table row just as userName does. However +securityName can be made human-readable, also it is not an index in +usmUserTable, thus duplicate securityName parameters are possible. +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpEngine(), + UsmUserData('usr-md5-none', 'authkey1', securityName='myuser'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'))) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py new file mode 100644 index 00000000..9bc76446 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py @@ -0,0 +1,35 @@ +""" +GET over IPv6 ++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols +* over IPv6/UDP +* to an Agent at [::1]:161 +* for three OIDs in string form + +Functionally similar to: + +| $ snmpget -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 udp6:[::1]:161 1.3.6.1.2.1.1.1.0 1.3.6.1.2.1.1.2.0 1.3.6.1.2.1.1.3.0 +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpEngine(), + UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + Udp6TransportTarget(('::1', 161)), + ContextData(), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0')), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.2.0')), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.3.0'))) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py new file mode 100644 index 00000000..ed5d8351 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py @@ -0,0 +1,36 @@ +""" +GET table row ++++++++++++++ + +Send SNMP GET request 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 IF-MIB::ifInOctets.1 and IF-MIB::ifOutOctets.1 MIB object +* perform response OIDs and values resolution at MIB + +Functionally similar to: + +| $ snmpget -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 demo.snmplabs.com IF-MIB::ifInOctets.1 IF-MIB::ifOutOctets.1 + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpEngine(), + UsmUserData('usr-none-none'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1)), + ObjectType(ObjectIdentity('IF-MIB', 'ifOutOctets', 1))) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py new file mode 100644 index 00000000..672aea93 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py @@ -0,0 +1,39 @@ +""" +Fetch table row by composite index +++++++++++++++++++++++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv3, user 'usr-sha-aes128', SHA auth, AES128 privacy +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for TCP-MIB::tcpConnLocalAddress."0.0.0.0".22."0.0.0.0".0 MIB object + +Functionally similar to: + +| $ snmpget -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 -a SHA -x AES demo.snmplabs.com TCP-MIB::tcpConnLocalAddress."0.0.0.0".22."0.0.0.0".0 + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpEngine(), + UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', + authProtocol=usmHMACSHAAuthProtocol, + privProtocol=usmAesCfb128Protocol), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('TCP-MIB', + 'tcpConnLocalAddress', + '0.0.0.0', 22, + '0.0.0.0', 0))) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py new file mode 100644 index 00000000..499c9b02 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py @@ -0,0 +1,42 @@ +""" +Fetch scalar and table variables +++++++++++++++++++++++++++++++++ + +Send a series of SNMP GETBULK requests using the following options: + +* with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols +* over IPv6/UDP +* to an Agent at [::1]:161 +* with values non-repeaters = 1, max-repetitions = 25 +* for IP-MIB::ipAdEntAddr and all columns of the IF-MIB::ifEntry table +* stop when response OIDs leave the scopes of the table + +Functionally similar to: + +| $ snmpbulkwalk -v3 -lauthPriv -u usr-md5-des -A authkey1 -X privkey1 -Cn1, -Cr25 demo.snmplabs.com IP-MIB::ipAdEntAddr IP-MIB::ipAddrEntry + +"""# +from pysnmp.hlapi import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in bulkCmd(SnmpEngine(), + UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + Udp6TransportTarget(('::1', 161)), + ContextData(), + 1, 25, + ObjectType(ObjectIdentity('IP-MIB', 'ipAdEntAddr')), + ObjectType(ObjectIdentity('IP-MIB', 'ipAddrEntry')), + lexicographicMode=False): + + if errorIndication: + print(errorIndication) + break + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + break + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py new file mode 100644 index 00000000..dd94650c --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py @@ -0,0 +1,41 @@ +""" +Walk Agent, limit number of packets ++++++++++++++++++++++++++++++++++++ + +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 OR + maxCalls == 10 request-response interactions occur + +Functionally similar to: + +| $ snmpbulkwalk -v3 -lnoAuthNoPriv -u usr-none-none -Cn0 -Cr50 demo.snmplabs.com SNMPv2-MIB::system + +"""# +from pysnmp.hlapi import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in bulkCmd(SnmpEngine(), + UsmUserData('usr-none-none'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + 0, 50, + ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')), + maxCalls=10): + + if errorIndication: + print(errorIndication) + break + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + break + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-variables.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-variables.py new file mode 100644 index 00000000..fa39086b --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-variables.py @@ -0,0 +1,43 @@ +""" +Walk MIB, limit number of response rows ++++++++++++++++++++++++++++++++++++++++ + +Send a series of SNMP GETBULK requests using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* with values non-repeaters = 0, max-repetitions = 25 +* for two OIDs in string form +* stop when response OIDs leave the scopes of initial OIDs OR + number of response rows reach fixed value (20) + +Functionally similar to: + +| $ snmpbulkwalk -v2c -c public demo.snmplabs.com -Cn0 -Cr25 1.3.6.1.2.1.2.2 1.3.6.1.2.1.2.3 + +"""# +from pysnmp.hlapi import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in bulkCmd(SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + 0, 25, + ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2')), + ObjectType(ObjectIdentity('1.3.6.1.2.1.2.3')), + lexicographicMode=False, maxRows=20): + + if errorIndication: + print(errorIndication) + break + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex)-1][0] or '?')) + break + else: + for varBind in varBinds: + print(' = '.join([ x.prettyPrint() for x in varBind ])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getnext-limit-number-of-variables.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getnext-limit-number-of-variables.py new file mode 100644 index 00000000..f9fd70c1 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getnext-limit-number-of-variables.py @@ -0,0 +1,42 @@ +""" +Fetch fixed amount of MIB variables ++++++++++++++++++++++++++++++++++++ + +Send a series of SNMP GETNEXT requests using the following options: + +* with SNMPv3, user 'usr-sha-aes128', SHA auth, AES128 privacy +* over UDP/IPv4 +* to an Agent at demo.snmplabs.com:161 +* for all OIDs past SNMPv2-MIB +* run till end-of-mib condition is reported by Agent OR maxRows == 100 +* ignoring non-increasing OIDs whenever reported by Agent + +Functionally similar to: + +| $ snmpwalk -v3 -lauthPriv -u usr-sha-aes128 -A authkey1 -X privkey1 -a MD5 -x AES demo.snmplabs.com SNMPv2-MIB::system + +"""# +from pysnmp.hlapi import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in nextCmd(SnmpEngine(), + UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', + authProtocol=usmHMACSHAAuthProtocol, + privProtocol=usmAesCfb128Protocol), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB')), + maxRows=100, ignoreNonIncreasingOid=True): + + if errorIndication: + print(errorIndication) + break + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + break + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getnext-multiple-oids-to-eom.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getnext-multiple-oids-to-eom.py new file mode 100644 index 00000000..863ceb6f --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getnext-multiple-oids-to-eom.py @@ -0,0 +1,40 @@ +""" +Fetch two OID branches +++++++++++++++++++++++ + +Send a series of SNMP GETNEXT requests using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for two OIDs in string form +* stop when response OIDs leave the scopes of initial OIDs + +Functionally similar to: + +| $ snmpwalk -v2c -c public demo.snmplabs.com 1.3.6.1.2.1.2.2.1.2 1.3.6.1.2.1.2.2.1.3 + +"""# +from pysnmp.hlapi import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in nextCmd(SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.2')), + ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.3')), + lexicographicMode=False): + + if errorIndication: + print(errorIndication) + break + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex)-1][0] or '?')) + break + else: + for varBind in varBinds: + print(' = '.join([ x.prettyPrint() for x in varBind ])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py new file mode 100644 index 00000000..d883d16a --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py @@ -0,0 +1,44 @@ +""" +Sequence Of GET's ++++++++++++++++++ + +Send two SNMP GET requests in a row 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 IF-MIB::ifInOctets.1 and IF-MIB::ifOutOctets.1 MIB objects +* with MIB lookup enabled + +Use a queue of MIB objects to query. + +The next() call is used to forward Python iterator to the position where it +could consume input + +Functionally similar to: + +| $ snmpget -v3 -l authNoPriv -u usr-md5-none -A authkey1 demo.snmplabs.com IF-MIB::ifInOctets.1 + +"""# +from pysnmp.hlapi import * + +queue = [[ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1))], + [ObjectType(ObjectIdentity('IF-MIB', 'ifOutOctets', 1))]] + +iterator = getCmd(SnmpEngine(), + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData()) + +next(iterator) + +while queue: + errorIndication, errorStatus, errorIndex, varBinds = iterator.send(queue.pop()) + if errorIndication: + print(errorIndication) + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py new file mode 100644 index 00000000..a9bb2bb3 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py @@ -0,0 +1,38 @@ +""" +Preload PySNMP MIBs ++++++++++++++++++++ + +Send a series of SNMP GETNEXT requests using the following options: + +* with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for all OIDs starting from 1.3.6 +* preload all Python MIB modules found in search path + +Functionally similar to: + +| $ snmpwalk -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 -m ALL demo.snmplabs.com:161 1.3.6 + +"""# +from pysnmp.hlapi import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in nextCmd(SnmpEngine(), + UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('1.3.6').loadMibs())): + + if errorIndication: + print(errorIndication) + break + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + break + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py new file mode 100644 index 00000000..b9103de3 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py @@ -0,0 +1,37 @@ +""" +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 + +Functionally similar to: + +| $ snmpwalk -v3 -lauthPriv -u usr-md5-none -A authkey1 -X privkey1 demo.snmplabs.com IF-MIB:: + +"""# +from pysnmp.hlapi import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in nextCmd(SnmpEngine(), + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB'))): + + if errorIndication: + print(errorIndication) + break + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + break + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py new file mode 100644 index 00000000..91a71a61 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py @@ -0,0 +1,44 @@ +""" +Fetch whole SNMP table +++++++++++++++++++++++ + +Send a series of SNMP GETNEXT requests using the following options: + +* with SNMPv1, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for some columns of the IF-MIB::ifEntry table +* stop when response OIDs leave the scopes of initial OIDs + +Functionally similar to: + +| $ snmpwalk -v1 -c public demo.snmplabs.com IF-MIB::ifDescr IF-MIB::ifType IF-MIB::ifMtu IF-MIB::ifSpeed IF-MIB::ifPhysAddress IF-MIB::ifType + +"""# +from pysnmp.hlapi import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in nextCmd(SnmpEngine(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')), + ObjectType(ObjectIdentity('IF-MIB', 'ifType')), + ObjectType(ObjectIdentity('IF-MIB', 'ifMtu')), + ObjectType(ObjectIdentity('IF-MIB', 'ifSpeed')), + ObjectType(ObjectIdentity('IF-MIB', 'ifPhysAddress')), + ObjectType(ObjectIdentity('IF-MIB', 'ifType')), + lexicographicMode=False): + + if errorIndication: + print(errorIndication) + break + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex)-1][0] or '?')) + break + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py new file mode 100644 index 00000000..16995ed8 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py @@ -0,0 +1,124 @@ +""" +Query Agents from multiple threads +++++++++++++++++++++++++++++++++++ + +Send a bunch of SNMP GET requests simultaneously using the following options: + +* process 5 GET requests in 3 parallel threads +* with SNMPv1, community 'public' and + with SNMPv2c, community 'public' and + with SNMPv3, user 'usr-md5-des', MD5 auth and DES privacy +* over IPv4/UDP and + over IPv6/UDP +* to an Agent at demo.snmplabs.com:161 and + to an Agent at [::1]:161 +* for instances of SNMPv2-MIB::sysDescr.0 and + SNMPv2-MIB::sysLocation.0 MIB objects + +"""# +from sys import version_info +from threading import Thread +from pysnmp.hlapi import * + +if version_info[0] == 2: + from Queue import Queue +else: + from queue import Queue + +# List of targets in the following format: +# ( ( authData, transportTarget, varNames ), ... ) +targets = ( + # 1-st target (SNMPv1 over IPv4/UDP) + (CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # 2-nd target (SNMPv2c over IPv4/UDP) + (CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # 3-nd target (SNMPv2c over IPv4/UDP) - same community and + # different transport address. + (CommunityData('public'), + UdpTransportTarget(('localhost', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysContact', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)))), + # 4-nd target (SNMPv3 over IPv4/UDP) + (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # 5-th target (SNMPv3 over IPv6/UDP) + (UsmUserData('usr-md5-none', 'authkey1'), + Udp6TransportTarget(('::1', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # N-th target + # ... +) + + +class Worker(Thread): + def __init__(self, requests, responses): + Thread.__init__(self) + self.snmpEngine = SnmpEngine() + self.requests = requests + self.responses = responses + self.setDaemon(True) + self.start() + + def run(self): + while True: + authData, transportTarget, varBinds = self.requests.get() + self.responses.append( + next(getCmd(self.snmpEngine, + authData, transportTarget, ContextData(), *varBinds)) + ) + if hasattr(self.requests, 'task_done'): # 2.5+ + self.requests.task_done() + + +class ThreadPool(object): + def __init__(self, num_threads): + self.requests = Queue(num_threads) + self.responses = [] + for _ in range(num_threads): + Worker(self.requests, self.responses) + + def addRequest(self, authData, transportTarget, varBinds): + self.requests.put((authData, transportTarget, varBinds)) + + def getResponses(self): + return self.responses + + def waitCompletion(self): + if hasattr(self.requests, 'join'): + self.requests.join() # 2.5+ + else: + from time import sleep + # this is a lame substitute for missing .join() + # adding an explicit synchronization might be a better solution + while not self.requests.empty(): + sleep(1) + + +pool = ThreadPool(3) + +# Submit GET requests +for authData, transportTarget, varBinds in targets: + pool.addRequest(authData, transportTarget, varBinds) + +# Wait for responses or errors +pool.waitCompletion() + +# Walk through responses +for errorIndication, errorStatus, errorIndex, varBinds in pool.getResponses(): + if errorIndication: + print(errorIndication) + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py new file mode 100644 index 00000000..be4ffeab --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py @@ -0,0 +1,43 @@ +""" +SET scalars values +++++++++++++++++++ + +Send SNMP SET request using the following options: + +* with SNMPv1, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* setting three var-bindings to new values + +Please note, that in this example MIB lookup is only used +for the second var-bindins. For the rest, value types are +inferred from passed objects. + +Functionally similar to: + +| $ snmpset -v1 -c public demo.snmplabs.com 1.3.6.1.2.1.1.9.1.2.1 o 1.3.6.1.4.1.20408.1.1 1.3.6.1.2.1.1.9.1.2.1 = 1.3.6.1.4.1.20408.1.1 1.3.6.1.2.1.1.9.1.3.1 s "new system name" + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + setCmd(SnmpEngine(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysORID', 1), + ObjectIdentifier('1.3.6.1.4.1.20408.1.1')), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.9.1.2.1'), + '1.3.6.1.4.1.20408.1.1'), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.9.1.3.1'), + OctetString('new system name'))) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/specific-v3-engine-id.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/specific-v3-engine-id.py new file mode 100644 index 00000000..94f591c4 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/specific-v3-engine-id.py @@ -0,0 +1,83 @@ +""" +Discover SNMPv3 SecurityEngineId +++++++++++++++++++++++++++++++++ + +Send SNMP GET request using the following scenario and options: + +* try to communicate with a SNMPv3 Engine using: + +* a non-existing user +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 + +* if remote SNMP Engine ID is discovered, send SNMP GET request: + +* with SNMPv3, user 'usr-md5-none', MD5 authentication, no privacy + at discovered securityEngineId +* to the same SNMP Engine ID +* for an OID in text form + +"""# +from pysnmp.hlapi import * + +snmpEngine = SnmpEngine() + +transportTarget = UdpTransportTarget(('demo.snmplabs.com', 161)) + +# +# To discover remote SNMP EngineID we will tap on SNMP engine inner workings +# by setting up execution point observer setup on INTERNAL class PDU processing +# + +observerContext = {} + +# Register a callback to be invoked at specified execution point of +# SNMP Engine and passed local variables at execution point's local scope +snmpEngine.observer.registerObserver( + lambda e, p, v, c: c.update(securityEngineId=v['securityEngineId']), + 'rfc3412.prepareDataElements:internal', + cbCtx=observerContext +) + +# Send probe SNMP request with invalid credentials + +authData = UsmUserData('non-existing-user') + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(snmpEngine, authData, transportTarget, ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))) +) + +# See if our SNMP engine received REPORT PDU containing securityEngineId + +if 'securityEngineId' not in observerContext: + print("Can't discover peer EngineID, errorIndication: %s" % errorIndication) + raise Exception() + +securityEngineId = observerContext.pop('securityEngineId') + +print('Remote securityEngineId = %s' % securityEngineId.prettyPrint()) + +# +# Query remote SNMP Engine using usmUserTable entry configured for it +# + +authData = UsmUserData('usr-md5-none', 'authkey1', + securityEngineId=securityEngineId) + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(snmpEngine, + authData, + transportTarget, + ContextData(), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'))) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-md5-des.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-md5-des.py new file mode 100644 index 00000000..27c0e7fb --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-md5-des.py @@ -0,0 +1,34 @@ +""" +SNMPv3: auth MD5, privacy DES ++++++++++++++++++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv3, user 'usr-md5-des', MD5 authentication, DES encryption +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for IF-MIB::ifInOctets.1 MIB object + +Functionally similar to: + +| $ snmpget -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 demo.snmplabs.com IF-MIB::ifInOctets.1 + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpEngine(), + UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1))) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-md5-none.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-md5-none.py new file mode 100644 index 00000000..f06964e2 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-md5-none.py @@ -0,0 +1,34 @@ +""" +SNMPv3: auth MD5, no privacy +++++++++++++++++++++++++++++ + +Send SNMP GET request 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 IF-MIB::ifInOctets.1 MIB object + +Functionally similar to: + +| $ snmpget -v3 -l authNoPriv -u usr-md5-none -A authkey1 demo.snmplabs.com IF-MIB::ifInOctets.1 + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpEngine(), + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1))) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-none-none.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-none-none.py new file mode 100644 index 00000000..940d8817 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-none-none.py @@ -0,0 +1,33 @@ +""" +SNMPv3: no auth, no privacy ++++++++++++++++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv3, user 'usr-none-none', no authentication, no encryption +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for IF-MIB::ifInOctets.1 MIB object + +Functionally similar to: + +| $ snmpget -v3 -l noAuthNoPriv -u usr-none-none demo.snmplabs.com IF-MIB::ifInOctets.1 + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpEngine(), + UsmUserData('usr-none-none'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1))) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-sha-aes128.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-sha-aes128.py new file mode 100644 index 00000000..53d4f800 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-sha-aes128.py @@ -0,0 +1,55 @@ +""" +SNMPv3: auth SHA, privacy AES128 +++++++++++++++++++++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv3, user 'usr-sha-aes', SHA authentication, AES128 encryption +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for SNMPv2-MIB::sysDescr.0 MIB object + +Available authentication protocols: + +#. usmHMACMD5AuthProtocol +#. usmHMACSHAAuthProtocol +#. usmHMAC128SHA224AuthProtocol +#. usmHMAC192SHA256AuthProtocol +#. usmHMAC256SHA384AuthProtocol +#. usmHMAC384SHA512AuthProtocol +#. usmNoAuthProtocol + +Available privacy protocols: + +#. usmDESPrivProtocol +#. usm3DESEDEPrivProtocol +#. usmAesCfb128Protocol +#. usmAesCfb192Protocol +#. usmAesCfb256Protocol +#. usmNoPrivProtocol + +Functionally similar to: + +| $ snmpget -v3 -l authPriv -u usr-sha-aes -A authkey1 -X privkey1 -a SHA -x AES demo.snmplabs.com SNMPv2-MIB::sysDescr.0 + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpEngine(), + UsmUserData('usr-sha-aes', 'authkey1', 'privkey1', + authProtocol=usmHMACSHAAuthProtocol, + privProtocol=usmAesCfb128Protocol), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v1-get.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v1-get.py new file mode 100644 index 00000000..1b272967 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v1-get.py @@ -0,0 +1,34 @@ +""" +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, + +Functionally similar to: + +| $ snmpget -v1 -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpEngine(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v2c-get.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v2c-get.py new file mode 100644 index 00000000..606bab5b --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v2c-get.py @@ -0,0 +1,35 @@ +""" +SNMPv2c ++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for two OIDs in string form + +Functionally similar to: + +| $ snmpget -v2c -c public demo.snmplabs.com 1.3.6.1.2.1.1.1.0 1.3.6.1.2.1.1.6.0 + +"""# +from pysnmp.hlapi import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + getCmd(SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0')), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.6.0'))) +) + +if errorIndication: + print(errorIndication) +elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) +else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v2c-getbulk.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v2c-getbulk.py new file mode 100644 index 00000000..47715195 --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v2c-getbulk.py @@ -0,0 +1,42 @@ +""" +Bulk walk MIB ++++++++++++++ + +Send a series of SNMP GETBULK requests using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* with values non-repeaters = 0, max-repetitions = 25 +* for two OIDs in string form +* stop when response OIDs leave the scopes of initial OIDs + +Functionally similar to: + +| $ snmpbulkwalk -v2c -c public demo.snmplabs.com -Cn0 -Cr25 1.3.6.1.2.1.2.2 1.3.6.1.2.1.2.3 + +"""# +from pysnmp.hlapi import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in bulkCmd(SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + 0, 25, + ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2')), + ObjectType(ObjectIdentity('1.3.6.1.2.1.2.3')), + lexicographicMode=False): + + if errorIndication: + print(errorIndication) + break + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + break + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/waive-mib-lookup.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/waive-mib-lookup.py new file mode 100644 index 00000000..f44d160b --- /dev/null +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/waive-mib-lookup.py @@ -0,0 +1,42 @@ +""" +Waive MIB lookup +++++++++++++++++ + +Perform SNMP GETNEXT operation with the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for an OID in string form +* do not resolve request/response OIDs and values from/toto human-friendly form + +The `lookupMib=False` keyword argument makes pysnmp NOT resolving +request and response variable-bindings from/to human-friendly form. + +Functionally similar to: + +| $ snmpwalk -v2c -c public -ObentU demo.snmplabs.com 1.3.6.1.2.1 + +"""# +from pysnmp.hlapi import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in nextCmd(SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1')), + lookupMib=False): + + if errorIndication: + print(errorIndication) + break + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + break + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/trollius/agent/ntforg/default-v1-trap.py b/examples/hlapi/v3arch/trollius/agent/ntforg/default-v1-trap.py new file mode 100644 index 00000000..64e61b1d --- /dev/null +++ b/examples/hlapi/v3arch/trollius/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' +* use trollius I/O framework + +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" + +"""# +import trollius +from pysnmp.hlapi.v3arch.asyncio import * + + +@trollius.coroutine +def run(): + snmpEngine = SnmpEngine() + (errorIndication, + errorStatus, + errorIndex, + varBinds) = yield trollius.From( + sendNotification( + snmpEngine, + CommunityData('public'), # mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData(), + 'inform', + 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')) + ) + ) + ) + + if errorIndication: + print(errorIndication) + elif errorStatus: + print('%s: at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) + + snmpEngine.transportDispatcher.closeDispatcher() + + +trollius.get_event_loop().run_until_complete(run()) diff --git a/examples/hlapi/v3arch/trollius/manager/cmdgen/getbulk-to-eom.py b/examples/hlapi/v3arch/trollius/manager/cmdgen/getbulk-to-eom.py new file mode 100644 index 00000000..f084e398 --- /dev/null +++ b/examples/hlapi/v3arch/trollius/manager/cmdgen/getbulk-to-eom.py @@ -0,0 +1,61 @@ +""" +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 trollius I/O framework + +Functionally similar to: + +| $ snmpbulkwalk -v3 -lnoAuthNoPriv -u usr-none-none -Cn0 -Cr50 demo.snmplabs.com SNMPv2-MIB::system + +"""# +import trollius +from pysnmp.hlapi.v3arch.asyncio import * + + +@trollius.coroutine +def run(varBinds): + snmpEngine = SnmpEngine() + while True: + (errorIndication, + errorStatus, + errorIndex, + varBindTable) = yield trollius.From( + bulkCmd(snmpEngine, + UsmUserData('usr-none-none'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + 0, 50, + *varBinds) + ) + + if errorIndication: + print(errorIndication) + break + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + else: + for varBindRow in varBindTable: + for varBind in varBindRow: + print(' = '.join([x.prettyPrint() for x in varBind])) + + varBinds = varBindTable[-1] + if isEndOfMib(varBinds): + break + + snmpEngine.transportDispatcher.closeDispatcher() + + +loop = trollius.get_event_loop() + +loop.run_until_complete( + run([ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr'))]) +) diff --git a/examples/hlapi/v3arch/trollius/manager/cmdgen/v1-get.py b/examples/hlapi/v3arch/trollius/manager/cmdgen/v1-get.py new file mode 100644 index 00000000..771e48c6 --- /dev/null +++ b/examples/hlapi/v3arch/trollius/manager/cmdgen/v1-get.py @@ -0,0 +1,48 @@ +""" +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 an instance of SNMPv2-MIB::sysDescr.0 MIB object + * Based on trollius I/O framework + +Functionally similar to: + +| $ snmpget -v1 -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 + +"""# +import trollius +from pysnmp.hlapi.v3arch.asyncio import * + + +@trollius.coroutine +def run(): + snmpEngine = SnmpEngine() + (errorIndication, + errorStatus, + errorIndex, + varBinds) = yield trollius.From( + getCmd(snmpEngine, + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))) + ) + + if errorIndication: + print(errorIndication) + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) + + snmpEngine.transportDispatcher.closeDispatcher() + + +trollius.get_event_loop().run_until_complete(run()) 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']) diff --git a/examples/v1arch/asyncore/agent/ntforg/send-inform-over-ipv4-and-ipv6.py b/examples/v1arch/asyncore/agent/ntforg/send-inform-over-ipv4-and-ipv6.py index 69d9b43b..43d37e5f 100644 --- a/examples/v1arch/asyncore/agent/ntforg/send-inform-over-ipv4-and-ipv6.py +++ b/examples/v1arch/asyncore/agent/ntforg/send-inform-over-ipv4-and-ipv6.py @@ -41,7 +41,6 @@ def cbTimerFun(timeNow): raise Exception("Request timed out") -# noinspection PyUnusedLocal,PyUnusedLocal def cbRecvFun(transportDispatcher, transportDomain, transportAddress, wholeMsg, reqPDU=reqPDU): while wholeMsg: -- cgit v1.2.1