Python 向PySNMP getCmd传递对象类型数组

Python 向PySNMP getCmd传递对象类型数组,python,pysnmp,Python,Pysnmp,我正在尝试将PySNMP用于监控系统,但我想为每个连接传递一个要查询的对象的动态列表,如下所示: errorIndication, errorStatus, errorIndex, varBinds = next( getCmd(SnmpEngine(), CommunityData(self.device.getSNMPCommunity(), mpModel=0), UdpTransportTarget((self.device.g

我正在尝试将PySNMP用于监控系统,但我想为每个连接传递一个要查询的对象的动态列表,如下所示:

errorIndication, errorStatus, errorIndex, varBinds = next(
        getCmd(SnmpEngine(),
           CommunityData(self.device.getSNMPCommunity(), mpModel=0),
           UdpTransportTarget((self.device.getHost(), 
self.device.getSNMPPort()),self.device.getSNMPTimeout(),int(1)),
           ContextData(),
           ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)),
           ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)),
           ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysUpTime', 0)),
           ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)),
           ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysContact', 0)),
        )
    )
相反,我希望能够做到以下几点:

for sensor in self.sensors:
        if(sensor.sensor_type == 'snmp'):
            if(sensor.snmp_oid):
                Sensors.append(ObjectType(ObjectIdentity(sensor.snmp_oid)))
            else:
                Sensors.append(
                    ObjectType(
                        ObjectIdentity(
                            sensor.snmp_mib,
                            sensor.snmp_object,
                            sensor.snmp_field
                     ).addAsn1MibSource('file:///usr/share/snmp/mibs')))
然后打电话

errorIndication, errorStatus, errorIndex, varBinds = next(
        getCmd(SnmpEngine(),
            CommunityData(device_to_proc.snmp_community, mpModel=0),
            UdpTransportTarget((device_to_proc.host, int(device_to_proc.snmp_port)),int(device_to_proc.snmp_timeout),int(1)),
            ContextData(),
            Sensors
        )
    )

我是否缺少pysnmp的另一个功能,或者有更好的方法来实现这一点?

我猜您需要的只是一个星号
*
来解压收集到SNMP GET函数varargs中的对象序列:

errorIndication, errorStatus, errorIndex, varBinds = next(
    getCmd(SnmpEngine(),
           CommunityData(device_to_proc.snmp_community, mpModel=0),
           UdpTransportTarget((device_to_proc.host, int(device_to_proc.snmp_port)),int(device_to_proc.snmp_timeout),int(1)),
           ContextData(),
           *Sensors
    )
)

作为补充说明,请记住,虽然您可以向SNMP代理发送的OID数量没有明确的限制,但一些代理可能会阻塞和/或拒绝在单个SNMP查询中提供太多的对象。

哇,我显然是一个Python noob,这就成功了!太棒了,谢谢你。