如何用不带auth no prv的python获取SNMPv3? 我试着用金龟子做这件事,但不起作用. 设备:交换机Cisco 2960

from pysnmp.hlapi import UsmUserData, usmHMACSHAAuthProtocol,usmAesCfb256Protocol
from pysnmp.hlapi import SnmpEngine, UdpTransportTarget, ContextData,ObjectType, ObjectIdentity, getCmd

snmpName =username
snmpPass = password
ipHost = ipAddress

auth=UsmUserData(userName=snmpName,authKey=snmpPass,authProtocol=usmHMACSHAAuthProtocol,privKey='',privProtocol=usmAesCfb256Protocol)


data = getCmd(SnmpEngine(),auth,UdpTransportTarget((ipHost, 161)),ContextData(),ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)))

errorIndication, errorStatus, errorIndex, varBinds = next(data)
print(errorIndication, errorStatus, errorIndex, varBinds)

当try 以任何方式打印此数据时,我都会出错 错误:

>>> errorIndication, errorStatus, errorIndex, varBinds = next(data)
Traceback (most recent call last):
  File "/home/alchimie/Documents/django/venv/lib/python3.10/site-packages/pysnmp/smi/builder.py", line 401, in loadModules
    self.loadModule(modName, **userCtx)
  File "/home/alchimie/Documents/django/venv/lib/python3.10/site-packages/pysnmp/smi/builder.py", line 377, in loadModule
    raise error.MibNotFoundError(
pysnmp.smi.error.MibNotFoundError: MIB file "IF-MIB.py[co]" not found in search path (DirMibSource('/home/alchimie/Documents/django/venv/lib/python3.10/site-packages/pysnmp/smi/mibs'), DirMibSource('/home/alchimie/Documents/django/venv/lib/python3.10/site-packages/pysnmp/smi/mibs/instances'), DirMibSource('pysnmp_mibs'), DirMibSource('/home/alchimie/.pysnmp/mibs'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/alchimie/Documents/django/venv/lib/python3.10/site-packages/pysnmp/hlapi/asyncore/sync/cmdgen.py", line 108, in getCmd
    cmdgen.getCmd(snmpEngine, authData, transportTarget,
  File "/home/alchimie/Documents/django/venv/lib/python3.10/site-packages/pysnmp/hlapi/asyncore/cmdgen.py", line 130, in getCmd
    vbProcessor.makeVarBinds(snmpEngine, varBinds), __cbFun,
  File "/home/alchimie/Documents/django/venv/lib/python3.10/site-packages/pysnmp/hlapi/varbinds.py", line 39, in makeVarBinds
    __varBinds.append(varBind.resolveWithMib(mibViewController, ignoreErrors=False))
  File "/home/alchimie/Documents/django/venv/lib/python3.10/site-packages/pysnmp/smi/rfc1902.py", line 853, in resolveWithMib
    self.__args[0].resolveWithMib(mibViewController)
  File "/home/alchimie/Documents/django/venv/lib/python3.10/site-packages/pysnmp/smi/rfc1902.py", line 480, in resolveWithMib
    mibNode, = mibViewController.mibBuilder.importSymbols(
  File "/home/alchimie/Documents/django/venv/lib/python3.10/site-packages/pysnmp/smi/builder.py", line 441, in importSymbols
    self.loadModules(modName, **userCtx)
  File "/home/alchimie/Documents/django/venv/lib/python3.10/site-packages/pysnmp/smi/builder.py", line 410, in loadModules
    raise error.MibNotFoundError('%s compilation error(s): %s' % (modName, errs))
pysnmp.smi.error.MibNotFoundError: IF-MIB compilation error(s): missingcaused by <class 'pysnmp.smi.error.MibNotFoundError'>: MIB file "IF-MIB.py[co]" not found in search path (DirMibSource('/home/alchimie/Documents/django/venv/lib/python3.10/site-packages/pysnmp/smi/mibs'), DirMibSource('/home/alchimie/Documents/django/venv/lib/python3.10/site-packages/pysnmp/smi/mibs/instances'), DirMibSource('pysnmp_mibs'), DirMibSource('/home/alchimie/.pysnmp/mibs'))

推荐答案

您的错误消息表明您没有安装MIB文件.pysnmp的当前版本是lextudio,而不是这个one.请确保您已安装更新版本.

这是一些基于较新版本的pysnmp的伪代码,如果您仍然收到相同的错误,请让我知道.

from pysnmp.hlapi import *

host_ip_address = ''

authentication = UsmUserData(
    userName= 'your_user_name',
    authKey= 'your_password',
    authProtocol=usmHMACSHAAuthProtocol,
    privKey='your_private_key',
    privProtocol=usmAesCfb256Protocol
)


iterator = getCmd(SnmpEngine(),
                  authentication,
                  UdpTransportTarget((host_ip_address, 161)),
                  ContextData(),
                  ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)))

errorIndication, errorStatus, errorIndex, varBinds = next(iterator)

if errorIndication:  # SNMP engine errors
    print(errorIndication)
else:
    if errorStatus:  # SNMP agent errors
        print('%s at %s' % (errorStatus.prettyPrint(), varBinds[int(errorIndex)-1] if errorIndex else '?'))
    else:
        for varBind in varBinds:  # SNMP response contents
            print(' = '.join([x.prettyPrint() for x in varBind]))

Python-3.x相关问答推荐

如何在Django中创建两个不同权限的用户?

字符串块数组:如何根据一个数组中的元素对另一个数组中的元素进行分组

无法使用诗词安装PyYaml

将列表项的极列水平分解为新列

visual studio代码窗口中未激活虚拟环境11

使用 Fetch 提交表单到 Django 视图

基于Pandas列动态创建分箱,以使观测值数量或计数占总计数的1%.

Pandas教程:如何更新行内数值的位置

将逗号分隔的字符串类型系列转换为整数列表 pandas

过滤阈值大小数据以使用 Pyspark 或 Python 读取

有没有办法使用重采样矢量化添加缺失的月份?

导入在不同目录中定义的函数

正则表达式从文本文件中捕获包含制表符/空格和子字符串的部分字符串

如何准确测定cv2的结果.在BW/黑白图像中查找对象?

具有函数值的 Python 3 枚举

在初始化之前禁用`__setattr__`的干净方法

如何注释一个以另一个函数作为参数的函数?

django - 值更改后自动更新日期

tkinter TclError:错误的文件类型使用 askopenfilename

交错4个相同长度的python列表