通过func将多个oid发送到pysnmp

时间:2014-03-19 15:26:41

标签: 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?

致以最诚挚的问候,

1 个答案:

答案 0 :(得分:1)

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

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

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