问题描述
我正在编写下面的示例代码,以通过SNMP为python pysnmp模块获取Cisco交换机信息。
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
解决方法
诊断此问题的最快方法是使用一个您知道有效的EXISTING实用程序,然后使用您认为应该有效的身份验证针对该IPADDRESS进行尝试。 如果该实用程序,例如。 NET-SNMP snmpwalk或snmpgetnext不起作用,那么您的问题可能不在代码中,而是您对如何访问SNMP代理的了解。例如。您确定社区字符串“ public”有效吗?
从成功开始工作总比从失败进行插值更好。