如何使用python snmpset字符串或整数值?

时间:2014-12-31 06:55:23

标签: python snmp net-snmp

我试图通过python使用snmpset设置值。

import netsnmp
snmpcfgfile=netsnmp.Varbind(".1.3.6.1.4.1.171.12.1.2.18.1.1.5.3", 
                            "switch.cfg","STRING")
netsnmp.snmpset(snmpcfgfile, Version=2, DestHost="10.0.112.4", Community="public")`

(2秒后我看到一个错误):TypeError:期望的字符串或Unicode对象,找到NoneType

价值不变。

我做错了什么?我应该如何纠正它?

追踪(请帮助理解其含义):

--Call--
> /usr/lib/python2.7/dist-packages/netsnmp/client.py(185)set()
-> def set(self, varlist):
(Pdb)
> /usr/lib/python2.7/dist-packages/netsnmp/client.py(186)set()
-> res = client_intf.set(self, varlist)
(Pdb)
--Call--
> /usr/lib/python2.7/dist-packages/netsnmp/client.py(94)__iter__()
-> def __iter__(self):
(Pdb)
> /usr/lib/python2.7/dist-packages/netsnmp/client.py(95)__iter__()
-> return iter(self.varbinds)
(Pdb)
--Return--
> /usr/lib/python2.7/dist-packages/netsnmp/client.py(95)__iter__()-><listite...c8096490>
-> return iter(self.varbinds)
(Pdb)
Traceback (most recent call last):
  File "./snmp-set.py", line 7, in <module>
    netsnmp.snmpset(snmpcfgfile, Version=2, DestHost="10.0.112.4", Community="public")
  File "/usr/lib/python2.7/dist-packages/netsnmp/client.py", line 228, in snmpset
    res = sess.set(var_list)
  File "/usr/lib/python2.7/dist-packages/netsnmp/client.py", line 187, in set
    return res
  File "/usr/lib/python2.7/dist-packages/netsnmp/client.py", line 187, in set
    return res
  File "/usr/lib/python2.7/bdb.py", line 49, in trace_dispatch
    return self.dispatch_line(frame)
  File "/usr/lib/python2.7/bdb.py", line 67, in dispatch_line
    self.user_line(frame)
  File "/usr/lib/python2.7/pdb.py", line 157, in user_line
    if self.bp_commands(frame):
  File "/usr/lib/python2.7/pdb.py", line 167, in bp_commands
    if getattr(self, "currentbp", False) and \
TypeError: expected string or Unicode object, NoneType found    

1 个答案:

答案 0 :(得分:0)

您可以尝试遵循this blog post的建议,并以字符串形式分解您的OID并以这种方式编制索引:

snmpcfgfile=netsnmp.Varbind("agentBscSwFile", "0", "switch.cfg", "STRING")

您还应该确保可以使用&#34; public&#34;来编写OID。社区,通常这个社区在默认出厂设置中默认为只读。

祝你好运,关于net-snmp python绑定的文档充其量只是参差不齐。