2011-04-08 54 views
0

我在管理器端收到「錯誤的版本或解碼錯誤」。我是 只是想知道下面的示例陷阱發送者代碼是否有缺失。「版本不正確或解碼錯誤」發送陷阱時

public class SampleTrapSender { 

     public static void main(String[] args) { 

       String address = "172.27.41.101"; 
       String port = "162"; 

      address = address + "/" + port; 
       System.out.println("Target adddress ...." + address); 

       try{ 

        Snmp snmp = new Snmp(new 
        DefaultUdpTransportMapping()); 

        PDU trap = new PDU(); 
        trap.setType(PDU.TRAP); 

        OID oid = new OID("1.3.6.1.4.1.4976.2.1.1.0"); 

        Variable var = new OctetString("Test Message"); 



        trap.add(new VariableBinding(oid, var)); 

        Address targetAddress = new UdpAddress(address); 
        CommunityTarget target = new CommunityTarget(); 
        target.setCommunity(new OctetString("")); 
        target.setVersion(SnmpConstants.version2c); 
        target.setAddress(targetAddress); 

        snmp.send(trap, target, null, null); 
        System.out.println("DONE......"); 

       }catch(IOException exception){ 
        exception.printStackTrace(); 
       } 

     } 
} 

回答

0

與嘗試,

PDUv1 pdu = new PDUv1(); 

我覺得你的代碼是正確的。但是你可以嘗試使用我的代碼:

try { 
      Address targetAddress = GenericAddress.parse("udp:"+serverIp+"/162"); 
      TransportMapping transport = new DefaultUdpTransportMapping(); 
      transport.listen(); 
      Snmp snmp = new Snmp(transport); 

      CommunityTarget target = new CommunityTarget(); 
      target.setCommunity(new OctetString("public")); 
      target.setAddress(targetAddress); 
      target.setVersion(SnmpConstants.version2c); 

      PDUv1 pdu = new PDUv1(); 
      pdu.setType(PDU.TRAP); 

      OID trap = new OID("1.3.6.1.6.3.1.1.4.1.0"); 
      VariableBinding vb = new VariableBinding(); 
      vb.setOid(trap); 
      vb.setVariable(new OctetString("1.3.6.1.4.1.9.9.46.2.0.11"));//11,10 
      pdu.add(vb); 


      VariableBinding vb2 = new VariableBinding(); 
      vb2.setOid(new OID("1.3.6.1.4.1.9.9.46.1.3.1.1.4.1.80")); 
      vb2.setVariable(new OctetString("VLAN0080")); 
      pdu.add(vb2); 

      snmp.send(pdu, target);   
} catch (IOException e) { 
      e.printStackTrace(); 
} 

我希望這將有助於。

+0

我仍然得到相同的錯誤。 – santro 2011-04-08 08:29:37

+0

我已更新我的答案。請看看它。 – nIKUNJ 2011-04-08 09:23:02

+0

感謝您的信息。 – santro 2011-04-08 09:31:31