2014-03-19 34 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

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