简体   繁体   English

通过func将多oid发送到pysnmp

[英]Send multi oid to pysnmp through func

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', '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 -_- 但这不起作用... pysnmp无法理解该请求-_-

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? 但是我必须在函数中编写每个OID,所以它非常没用,为什么我不能像我想的那样发送很多OID?

Best regards, 最好的祝福,

If your input oids is a sequence of Python strings, you should just pass it over to nextCmd() like this: 如果您的输入oid是Python字符串序列,则应将其传递给nextCmd(),如下所示:

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. 无需在OID中添加多余的引号或逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM