python - 如何通过身份验证在 Cisco 路由器中获取 SNMP OID 值
问题描述
我正在编写下面的示例代码,用于通过 SNMP 为 python pysnmp 模块获取 Cisco 交换机信息。
执行以下代码后,我收到“超时前未收到 SNMP 响应”。
from pysnmp.entity.rfc3413.oneliner import cmdgen
import time
#SNMP agent address
SNMP_AGENT_HOST = 'IPADDRESS' # IP adderess
#SNMP default port
SNMP_PORT = 161
#Add SNMP agent community here
SNMP_COMMUNITY = 'public'
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
cmdgen.CommunityData(SNMP_COMMUNITY),
cmdgen.UdpTransportTarget((SNMP_AGENT_HOST, SNMP_PORT)),
'1.3.6.1.4.1.9.2.1.56.0', # Cisco Switch OID
'1.3.6.1.4.1.9.2.1.57.0' #
)
# Check for errors and print out results
if errorIndication:
print(errorIndication)
else:
if errorStatus:
print('%s at %s' % (
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex)-1] or '?'
)
)
else:
for name, val in varBinds:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
我得到结果
No SNMP response received before timeout
我想通过 GET 调用获取所有与 OID 相关的信息。
解决方案
诊断此问题的最快方法是使用您知道有效的现有实用程序,并使用您认为应该有效的身份验证针对该 IPADDRESS 进行尝试。如果该实用程序,例如。NET-SNMP snmpwalk 或 snmpgetnext,不起作用,那么您的问题可能不在您的代码中,而是您对如何访问 SNMP 代理的理解。例如。你确定社区字符串“public”有效吗?
从成功开始工作总是比从失败中插值更好。