在pysnmp中使用setValue

时间:2018-04-20 05:44:25

标签: pysnmp setvalue

我正在配置社区并尝试在pysnmp中使用setValue但它无法工作:

...

config.addV1System(snmpEngine, 'read-area', 'public')
config.addV1System(snmpEngine, 'write-area', 'private')
config.addVacmUser(snmpEngine, 2, 'read-area', 'noAuthNoPriv', (1, 3, 6, 5))
config.addVacmUser(snmpEngine, 2, 'write-area', 'noAuthNoPriv', (1, 3, 6, 5, 1, 0), (1, 3, 6, 5, 1, 0))

...

class MyStaticMibScalarInstance1(MibScalarInstance):
    def getValue(self, name, idx):
        return self.getSyntax().clone('111')
    def setValue(self, value, name, idx):
        print("111 %s %s %s\n".format(value, name, idx))

mibBuilder.exportSymbols('__MY_MIB', MibScalar((1, 3, 6, 5, 1), v2c.OctetString()), MyStaticMibScalarInstance1((1, 3, 6, 5, 1), (0,), v2c.OctetString()))

...

测试用例:

$ snmpwalk -v 2c -c public 127.0.0.1 1.3.6.5
    iso.3.6.5.1.0 = STRING: "111"
    iso.3.6.5.2.0 = No more variables left in this MIB View (It is past the end of the MIB tree)

$ snmpset -v 2c -c private 127.0.0.1 1.3.6.5.1.0 s test
    Error in packet. Reason: notWritable (That object does not support modification)
    Failed object: iso.3.6.5.1.0

如何启用私有社区并使setValue正常工作?

1 个答案:

答案 0 :(得分:0)

除了VACM设置(基于每个用户/社区),您还需要指出托管对象原则上是可写的:

mibBuilder.exportSymbols(
    '__MY_MIB',
    MibScalar((1, 3, 6, 5, 1), v2c.OctetString()).setMaxAccess('readwrite'),
    MyStaticMibScalarInstance1((1, 3, 6, 5, 1), (0,), v2c.OctetString())
)

可替换地:

class ManagedObject(MibScalar):
    maxAccess = 'readwrite'

mibBuilder.exportSymbols(
    '__MY_MIB',
    ManagedObject((1, 3, 6, 5, 1), v2c.OctetString()),
    MyStaticMibScalarInstance1((1, 3, 6, 5, 1), (0,), v2c.OctetString())
)