File: operations-on-managed-objects.py

package info (click to toggle)
python-pysnmp4 7.1.22-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,504 kB
  • sloc: python: 33,673; makefile: 169; javascript: 4
file content (61 lines) | stat: -rw-r--r-- 1,763 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Agent operations on MIB
+++++++++++++++++++++++

This script explains how SNMP Agent application manipulates
its MIB possibly triggered by SNMP Manager's commands.

"""  #
# SNMP agent backend e.g. Agent access to Managed Objects
from pysnmp.smi import builder, instrum, exval

print("Loading MIB modules..."),
mibBuilder = builder.MibBuilder().load_modules(
    "SNMPv2-MIB", "SNMP-FRAMEWORK-MIB", "SNMP-COMMUNITY-MIB"
)
print("done")

print("Building MIB tree..."),
mibInstrum = instrum.MibInstrumController(mibBuilder)
print("done")

print("Building table entry index from human-friendly representation..."),
(snmpCommunityEntry,) = mibBuilder.import_symbols(
    "SNMP-COMMUNITY-MIB", "snmpCommunityEntry"
)
instanceId = snmpCommunityEntry.getInstIdFromIndices("my-router")
print("done")

print("Create/update SNMP-COMMUNITY-MIB::snmpCommunityEntry table row: ")
varBinds = mibInstrum.write_variables(
    (snmpCommunityEntry.name + (2,) + instanceId, "mycomm"),
    (snmpCommunityEntry.name + (3,) + instanceId, "mynmsname"),
    (snmpCommunityEntry.name + (7,) + instanceId, "volatile"),
)
for oid, val in varBinds:
    print(
        "{} = {}".format(
            ".".join([str(x) for x in oid]),
            not val.isValue and "N/A" or val.prettyPrint(),
        )
    )
print("done")

print("Read whole MIB (table walk)")
varBinds = [((), None)]
while True:
    varBinds = mibInstrum.read_next_variables(*varBinds)
    oid, val = varBinds[0]
    if exval.endOfMib.isSameTypeWith(val):
        break
    print(
        "{} = {}".format(
            ".".join([str(x) for x in oid]),
            not val.isValue and "N/A" or val.prettyPrint(),
        )
    )
print("done")

print("Unloading MIB modules..."),
mibBuilder.unload_modules()
print("done")