pysnmp nextCmd的异常处理

时间:2019-01-17 02:47:22

标签: python pysnmp

从与MIB中的OID不对应的主机获取响应

run_snmp_discovery.py --host host.com --community string --command walk --mib_to_oid '{"OLD-CISCO-CHASSIS-MIB" : [ "cardIndex" ] }' --verbose
OLD-CISCO-CHASSIS-MIB::cardIndex.1 = 1
OLD-CISCO-CHASSIS-MIB::cardIndex.4 = 4
OLD-CISCO-CHASSIS-MIB::cardIndex.5 = 5
{"errors": {"PySnmpError": "PySnmp Exception for host: host.com, for OID: cardIndex"}}

编写代码块的目的是为不会出错的OID提供结果。但是,这不起作用-即使cardType实例不能解析为MIB中的条目,cardIndex,cardSlot等也应返回结果。

grep 1269 ~/.pysnmp/mibs/OLD-CISCO-CHASSIS-MIB.py*

grep 1269 /usr/share/snmp/mibs/OLD-CISCO-CHASSIS-MIB.mib
                        ,cpu-3900SPE200-4ge(1269)               -- cisco 3900 Services Performance Engine 200 (SPE200) with 4 GE, 3 EHWIC, 3 DSP

snmpwalk -v2c -c string host.com OLD-CISCO-CHASSIS-MIB::cardType
OLD-CISCO-CHASSIS-MIB::cardType.1 = INTEGER: cpu-3900SPE200-4ge(1269)
OLD-CISCO-CHASSIS-MIB::cardType.4 = INTEGER: pwr-c3900pwr-ac(4756)
OLD-CISCO-CHASSIS-MIB::cardType.5 = INTEGER: pwr-c3900pwr-ac(4756)


for mib, oids in mib_to_oids.iteritems():
            for oid in oids:
                g = nextCmd(SnmpEngine(), CommunityData(self.community), UdpTransportTarget((self.host, self.port), timeout, retries),
                            ContextData(),ObjectType(ObjectIdentity(mib, oid)),lexicographicMode=lexicographic_mode)
                while True:
                    try:
                        errorIndication, errorStatus, errorIndex, varBinds = next(g);
                    except StopIteration:
                        break
                    except PySnmpError:
                        _errors['errors']['PySnmpError'] = "PySnmp Exception for host: {0}, for OID: {1}".format(self.host, oid)
                        return json.dumps(_errors)

如何构造这种结构以允许其他OID继续运行?

0 个答案:

没有答案