简体   繁体   English

如何使用PySNMP获取SNMP表

[英]How to get SNMP-table using PySNMP

I'm trying to get information about the neighbors of the device from CDP-Table, but i get data about only one neighbor. 我正在尝试从CDP表获取有关设备邻居的信息,但我仅获得有关一个​​邻居的数据。 For example, I know for sure that the device has two neighbors, I make a request to cdpCacheDeviceId, and i get the ID of one. 例如,我确定该设备有两个邻居,我向cdpCacheDeviceId发出请求,并且我得到一个ID。 How do I get the ID of both devices? 如何获得两个设备的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

Thats what i recieve in output: 多数民众赞成在我收到的输出:

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 

As Ilya Etingof said, here was a problem with return with incorrect tabulation. 正如Ilya Etingof所说,这是制表不正确的return问题。 Here is the working code: 这是工作代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM