python net-snmp加载mibs

标签 python net-snmp mib pysnmp

我正在使用 net-snmp 的 python 库对各种交换机进行一些长时间的查询。我希望能够加载新的 mib——但我找不到任何关于如何执行此操作的文档。

PySNMP 似乎相当复杂,需要我为每个 mib 创建 Python 对象(这对我来说不适用);所以我坚持使用 net-snmp 的库(除了加载 mib 之外它们还不错)。

我知道我可以在 net-snmp 命令行工具中使用 -m-M 选项,并且有关于预编译 net-snmp 的文档套件(./configuremake 等)和所有的 mib(我也假设进入库);如果 Python 库不提供加载 mib 的能力,我是否可以至少配置 net-snmp 以提供我的 python 库访问 mib 而无需重新编译?

最佳答案

我终于找到了答案。来自 snmpcmd(1) 手册页:

   -m MIBLIST
          Specifies a colon separated  list  of  MIB  modules  (not
          files)  to load for this application.  This overrides (or
          augments) the environment variable  MIBS,  the  snmp.conf
          directive  mibs,  and the list of MIBs hardcoded into the
          Net-SNMP library.

这里的关键部分是您可以像使用 -m 命令行选项一样使用 MIBS 环境变量...并且对此的支持是在图书馆层面实现。这意味着如果您在启动 Python 之前定义了 MIBS 环境变量,它将影响 netsnmp 库的行为:

$ python 
Python 2.7.2 (default, Oct 27 2011, 01:40:22) 
[GCC 4.6.1 20111003 (Red Hat 4.6.1-10)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import netsnmp
>>> os.environ['MIBS'] = 'UPS-MIB:SNMPv2-SMI'
>>> oid = netsnmp.Varbind('upsAlarmOnBattery.0')
>>> netsnmp.snmpget(oid, Version=1, DestHost='myserver', Community='public')
('0',)
>>> 

请注意,您必须设置 os.environ['MIBS'] before 调用任何 netsnmp 模块函数(因为这将加载之后的库和任何环境变化都不会产生影响。

您(显然)也可以在 Python 之外设置环境变量:

$ export MIBS='UPS-MIB:SNMPv2-SMI'
$ python
>>> import netsnmp
>>> oid = netsnmp.Varbind('upsAlarmOnBattery.0')
>>> netsnmp.snmpget(oid, Version=1, DestHost='myserver', Community='public')
('0',)
>>> 

关于python net-snmp加载mibs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7923548/

相关文章:

windows - 如何将第三方 MIB 文件添加到 Windows SNMP?

python - 仅取消堆叠或旋转某些列

python - 我的 Tetration(复数)函数必须更好地矢量化(Python)

python - 无法在 centos 6.5 上的 python 中导入 netsnmp

go - GoSNMP 中的对象名称而不是 OID

c++ - 使用 Net-SNMP 的 snmpset 的多个值

python - QLabel 未显示在 QWidget 窗口中

python - 为什么我无法在 Python 脚本中导入模块,但可以在 REPL 中导入它?

java - SNMP 代理和 SNMP 适配器有什么区别

网络流量的 SNMP OID