質問

Currently I have this :

        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

Print displays :

'1.3.6.1.2.1.31.1.1.1.18','1.3.6.1.2.1.2.2.1.2',

But it doesn't work... pysnmp doesn't understand the request -_-

Else this solution works :

        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

But I have to write each OIDin my function, so it's very useless, why I can't send a lot of OID like I want to do?

Best regards,

役に立ちましたか?

解決

If your input oids is a sequence of Python strings, you should just pass it over to nextCmd() like this:

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

There's no need to add extra quotes or commas to OIDs.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top