2014-03-19 35 views
0

到pysnmp目前我有这样的:发送多OID通过FUNC

 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 

但是我必须编写每个OIDin我的函数,所以这是非常没用的,为什么我不能发送像我想要做的很多OID?

最好的问候,

回答

1

如果你输入的OID是Python中的序列,你应该只将它传递给nextCmd()这样的:

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

没有必要增加额外的报价或逗号给OID。

+0

好,它的工作原理,谢谢:) –