我试图从CDP-Table获取有关设备邻居的信息,但我只获得有关一个邻居的数据。例如,我确定设备有两个邻居,我向cdpCacheDeviceId发出请求,我得到一个ID。如何获取两台设备的ID?
def get_cdp_tables(host):
for (errorIndication,
errorStatus,
errorIndex,
varBinds) in nextCmd(SnmpEngine(),
CommunityData(''),
UdpTransportTarget((host, 161)),
ContextData(),
# cdpCacheTable
ObjectType(ObjectIdentity('1.0.8802.1.1.2.1.4.1.1.7')), # cdpCacheDevicePort
ObjectType(ObjectIdentity('1.3.6.1.4.1.9.9.23.1.2.1.1.17')), # cdpCacheSysName
ObjectType(ObjectIdentity('1.3.6.1.4.1.9.9.23.1.2.1.1.1')), # cdpCacheIfIndex
ObjectType(ObjectIdentity('1.3.6.1.4.1.9.9.23.1.2.1.1.6')), # cdpCacheDeviceId
ObjectType(ObjectIdentity('1.3.6.1.4.1.9.9.23.1.2.1.1.4')), # cdpCacheAddress
lexicographicMode=False):
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
with open('cdp.txt', 'a', 1) as cdp_file:
cdp_file.write(host + '\n')
for i in range(len(varBinds)):
cdp_file.write(str(varBinds[i]) + '\n')
return
这就是我在输出中收到的内容:
SNMPv2-SMI::iso.2.840.10006.300.43.1.2.1.1.2.1 = 32768
SNMPv2-SMI::enterprises.9.9.23.1.2.1.1.17.1.3 =
SNMPv2-SMI::enterprises.9.9.23.1.2.1.1.3.1.3 = 1
SNMPv2-SMI::enterprises.9.9.23.1.2.1.1.6.1.3 = c3750X
SNMPv2-SMI::enterprises.9.9.23.1.2.1.1.4.1.3 = 0xac1404fe
答案 0 :(得分:1)
正如Ilya Etingof所说,这是return
表格不正确的问题。这是工作代码:
def get_cdp_tables(host):
for (errorIndication,
errorStatus,
errorIndex,
varBinds) in nextCmd(SnmpEngine(),
CommunityData(''),
UdpTransportTarget((host, 161)),
ContextData(),
# cdpCacheTable
ObjectType(ObjectIdentity('1.0.8802.1.1.2.1.4.1.1.7')), # cdpCacheDevicePort
ObjectType(ObjectIdentity('1.3.6.1.4.1.9.9.23.1.2.1.1.17')), # cdpCacheSysName
ObjectType(ObjectIdentity('1.3.6.1.4.1.9.9.23.1.2.1.1.1')), # cdpCacheIfIndex
ObjectType(ObjectIdentity('1.3.6.1.4.1.9.9.23.1.2.1.1.6')), # cdpCacheDeviceId
ObjectType(ObjectIdentity('1.3.6.1.4.1.9.9.23.1.2.1.1.4')), # cdpCacheAddress
lexicographicMode=False):
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
with open('cdp.txt', 'a', 1) as cdp_file:
cdp_file.write(host + '\n')
for i in range(len(varBinds)):
cdp_file.write(str(varBinds[i]) + '\n')
return