File: setgen.py

package info (click to toggle)
python-pysnmp4 4.1.6a-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,016 kB
  • ctags: 1,826
  • sloc: python: 9,809; sh: 60; makefile: 11
file content (50 lines) | stat: -rw-r--r-- 1,542 bytes parent folder | download
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
# SET Command Generator with MIB resolution
import string
from pysnmp.entity.rfc3413.oneliner import cmdgen

cmdGen = cmdgen.CommandGenerator()

# Lookup Managed Object name & value at MIB
oid, suffix = cmdgen.mibvar.mibNameToOid(
    cmdGen.mibViewController,
    (('SNMPv2-MIB', 'sysDescr'), 0)
    )
val = cmdgen.mibvar.cloneFromMibValue(
    cmdGen.mibViewController,
    'SNMPv2-MIB', 'sysDescr', 'Some system desc'
    )

errorIndication, errorStatus, errorIndex, \
                 varBinds = cmdGen.setCmd(
    # SNMP v1
#    cmdgen.CommunityData('test-agent', 'public', 0),
    # SNMP v2
#    cmdgen.CommunityData('test-agent', 'public'),
    # SNMP v3
    cmdgen.UsmUserData('test-user', 'authkey1', 'privkey1'),
    # Transport
    cmdgen.UdpTransportTarget(('localhost', 161)),
    # Request variable(s)
    (oid + suffix, val)
    )

if errorIndication:
    print errorIndication
else:
    if errorStatus:
        print '%s at %s\n' % (
            errorStatus.prettyPrint(), varBinds[int(errorIndex)-1]
            )
    else:
        for oid, val in varBinds:
            (symName, modName), indices = cmdgen.mibvar.oidToMibName(
                cmdGen.mibViewController, oid
                )
            val = cmdgen.mibvar.cloneFromMibValue(
                cmdGen.mibViewController, modName, symName, val
                )
            print '%s::%s.%s = %s' % (
                modName, symName,
                string.join(map(lambda v: v.prettyPrint(), indices), '.'),
                val.prettyPrint()
                )