1

現在、私はこれを持っています:

        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 を書かなければならないので、それはとても役に立たないのです。

よろしくお願いします、

4

1 に答える 1

2

入力 oids が Python 文字列のシーケンスである場合、次のように nextCmd() に渡す必要があります。

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

OID に余分な引用符やカンマを追加する必要はありません。

于 2014-03-19T17:52:02.197 に答える