[英]PySNMP 4.4 with Python 2.7 bulkCmd output not including child OID's
首先,我是Python和PySNMP的新手。 我正在嘗試將網絡設備列表傳遞給bulkCmd以獲取有關所有物理接口的信息。
當前,它僅收集第一個接口,然后移至列表中的下一個網絡設備。 我對詞典和maxCalls進行了更改,但重復沒有變化。
將單個bulkCmd發送到單個網絡設備時,我已成功輪詢所有接口。
碼:
from pysnmp.hlapi import *
routers = ["router1", "router2"]
#adds routers to getCmd and bulkCmd
def snmpquery (hostip):
errorIndication, errorStatus, errorIndex, varBinds = next (
bulkCmd(SnmpEngine(),
CommunityData('communitystring'),
UdpTransportTarget((hostip, 161)),
ContextData(),
0, 50,
ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')),
ObjectType(ObjectIdentity('IF-MIB', 'ifAlias')),
ObjectType(ObjectIdentity('IF-MIB', 'ifOperStatus')),
lexicographicMode=True
)
)
# Check for errors and print out results
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))
# calls snmpquery for all routers in list
for router in routers:
snmpquery(router)
輸出:
IF-MIB::ifDescr.1 = GigabitEthernet0/0
IF-MIB::ifAlias.1 = InterfaceDesc
IF-MIB::ifOperStatus.1 = 'up'
IF-MIB::ifDescr.1 = GigabitEthernet0/0
IF-MIB::ifAlias.1 = InterfaceDesc
IF-MIB::ifOperStatus.1 = 'up'
bulkSNMP
返回一個迭代器,並且您正在其上使用next()
,它僅檢索第一個迭代。 您可能從PySNMP文檔中得到了這個主意,該文檔在顯示如何檢索所有結果方面做得並不好。
您應該使用for循環遍歷所有迭代,如下所示:
from pysnmp.hlapi import *
routers = ["router1", "router2"]
def snmpquery (hostip):
snmp_iter = bulkCmd(SnmpEngine(),
CommunityData('communitystring'),
UdpTransportTarget((hostip, 161)),
ContextData(),
0, 50,
ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')),
ObjectType(ObjectIdentity('IF-MIB', 'ifAlias')),
ObjectType(ObjectIdentity('IF-MIB', 'ifOperStatus')),
lexicographicMode=True)
for errorIndication, errorStatus, errorIndex, varBinds in snmp_iter:
# Check for errors and print out results
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))
# calls snmpquery for all routers in list
for router in routers:
snmpquery(router)
另外,在發布與Python有關的問題時,請注意縮進,因為縮進非常重要。
您需要遍歷bulkCmd
函數產生的生成器,以重復SNMP查詢以提取不適合先前響應包的SNMP受管對象。 只要溝next()
調用和運行for
遍歷bulkCmd()
旁注1:如果要獲取位於MIB表列正下方的托管對象(例如IF-MIB::ifDescr
等),則可能不需要lexicographicMode=True
。
旁注2:如果您的網絡上有許多SNMP代理,則可以考慮通過與並行交談來加快數據檢索的過程。 您將使用相同的getBulk()
調用,只是底層網絡I / O進行了並行處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.