summaryrefslogtreecommitdiff
path: root/examples/smi/agent/operations-on-managed-objects.py
blob: 13d31494377cf5a5b132f76f6bde964fea9ce6cf (plain)
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
# SNMP agent backend e.g. Agent access to Managed Objects
from pysnmp.smi import builder, instrum, exval

print('Loading MIB modules...'),
mibBuilder = builder.MibBuilder().loadModules(
    '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.importSymbols(
    'SNMP-COMMUNITY-MIB', 'snmpCommunityEntry'
)
instanceId = snmpCommunityEntry.getInstIdFromIndices('my-router')
print('done')

print('Create/update SNMP-COMMUNITY-MIB::snmpCommunityEntry table row: ')
varBinds = mibInstrum.writeVars(
    ( (snmpCommunityEntry.name+(2,)+instanceId, 'mycomm'),
      (snmpCommunityEntry.name+(3,)+instanceId, 'mynmsname'),
      (snmpCommunityEntry.name+(7,)+instanceId, 'volatile') )
)
for oid, val in varBinds:
    print('%s = %s' % ('.'.join([str(x) for x in oid]), val.prettyPrint()))
print('done')

print('Read whole MIB (table walk)')
oid, val = (), None
while 1:
    oid, val = mibInstrum.readNextVars(((oid, val),))[0]
    if exval.endOfMib.isSameTypeWith(val):
        break
    print('%s = %s' % ('.'.join([str(x) for x in oid]), val.prettyPrint()))
print('done')

print('Unloading MIB modules...'),
mibBuilder.unloadModules()
print('done')