【问题标题】:How to get SNMP-table using PySNMP如何使用 PySNMP 获取 SNMP 表
【发布时间】:2018-05-19 23:58:22
【问题描述】:

我正在尝试从 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 

【问题讨论】:

  • 最后一行的return 困扰着我——它会在第一次迭代时终止你的for 循环吗?
  • 是的,现在我收到了所有数据。非常感谢您的帮助,我真的很愚蠢,一直没有注意到返回。

标签: python snmp pysnmp


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多