如何使用 PySNMP 获取 SNMP-table
How to get SNMP-table using PySNMP
我正在尝试从 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
正如 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
我正在尝试从 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
正如 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