python - 通过func发送multi oid到pysnmp

标签 python pysnmp

目前我有这个:

        def snmp_request(self,*oids):
            my_oids =''
            for oid in oids:
                    my_oids += '\'' + oid + '\','
            print(my_oids)
            answer_list = list()
            cmdGen = cmdgen.CommandGenerator()
            errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
                    cmdgen.CommunityData(self.community),
                    cmdgen.UdpTransportTarget((self.ip, 161),20,1),
                    my_oids
            )
            if errorIndication:
                    return (errorIndication)
            else:
                    if errorStatus:
                            return ('%s at %s' % (
                            errorStatus.prettyPrint(),
                            errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
                                    )
                            )
                    else:
                            for varBindTableRow in varBindTable:
                                    for name, val in varBindTableRow:
                                            answer_list.append( val.prettyPrint())
            return answer_list

打印显示:

'1.3.6.1.2.1.31.1.1.1.18','1.3.6.1.2.1.2.2.1.2',

但它不起作用... pysnmp 不理解请求 -_-

否则这个解决方案有效:

        def snmp_request(self,*oids):
            my_oids =''
            for oid in oids:
                    my_oids += '\'' + oid + '\','
            print(my_oids)
            answer_list = list()
            cmdGen = cmdgen.CommandGenerator()
            errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
                    cmdgen.CommunityData(self.community),
                    cmdgen.UdpTransportTarget((self.ip, 161),20,1),
                    '1.3.6.1.2.1.31.1.1.1.18','1.3.6.1.2.1.2.2.1.2',
            )
            if errorIndication:
                    return (errorIndication)
            else:
                    if errorStatus:
                            return ('%s at %s' % (
                            errorStatus.prettyPrint(),
                            errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
                                    )
                            )
                    else:
                            for varBindTableRow in varBindTable:
                                    for name, val in varBindTableRow:
                                            answer_list.append( val.prettyPrint())
            return answer_list

但是我必须在我的函数中编写每个OID,所以它非常无用,为什么我不能像我想要的那样发送大量OID?

最诚挚的问候,

最佳答案

如果您的输入 oid 是 Python 字符串序列,您应该将其传递给 nextCmd(),如下所示:

errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
                cmdgen.CommunityData(self.community),
                cmdgen.UdpTransportTarget((self.ip, 161),20,1),
                *oids
)

无需向 OID 添加额外的引号或逗号。

关于python - 通过func发送multi oid到pysnmp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22510595/

相关文章:

python - 使用 Google App Engine (Python) 返回数据(json、xml 等)的 jQuery Post

python - "SyntaxError: Missing parentheses in call to ' print'"在Python中是什么意思?

python - 使用 pycharm 运行存储在数据库中的 python 脚本?如何?

python - 超过 1000 个主机的 pysnmp 扭曲客户端中的未处理错误

snmp - 使用pySNMP编写SNMP代理(适用于OpenNMS)

python - 部分敏感文件名导致数据加载失败

python 节俭错误 ```TSocket 读取 0 个字节 ```

Python SNMP 陷阱接收器

python - 除了 Udp6SocketTransport Pysnmp 之外还有其他替代方案吗

python - 如何在Python中使用PySnmp获取OID的值