Как получить SNMP-таблицу с помощью PySNMP - PullRequest
0 голосов
/ 18 мая 2018

Я пытаюсь получить информацию о соседях устройства из таблицы CDP, но я получаю данные только об одном соседе.Например, я точно знаю, что у устройства есть два соседа, я делаю запрос к cdpCacheDeviceId и получаю идентификатор одного.Как я могу получить 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 

1 Ответ

0 голосов
/ 19 мая 2018

Как сказал Илья Этингоф, здесь была проблема с 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
...