Programmaticaly使用SNMP4j以十六进制格式获取SNMP请求和响应

时间:2015-07-29 05:02:00

标签: snmp net-snmp snmp4j

我是SNMP4J世界的新手,并尝试使用SNMP4j管理设备。请在线查找代码。我需要十六进制格式的请求和响应数据有效负载。我能够获得请求和响应,但作为java对象。请找到输出

计划:

 public static void main(String[] args) throws Exception
   {
    TransportMapping transport = new DefaultUdpTransportMapping();
    transport.listen();
    CommunityTarget comtarget = new CommunityTarget();
    comtarget.setCommunity(new OctetString(community));
    comtarget.setVersion(snmpVersion);
    comtarget.setAddress(new UdpAddress(ipAddress + "/" + port));
    comtarget.setRetries(2);
    comtarget.setTimeout(1000);
    PDU pdu = new PDU();
    pdu.add(new VariableBinding(new OID(oidValue)));
    pdu.setType(PDU.GET);
    pdu.setRequestID(new Integer32(1));
    Snmp snmp = new Snmp(transport);

    System.out.println("Sending Request to Agent...");
    PDU requestPDU=snmp.get(pdu, comtarget).getRequest();

    System.out.println("request : "+requestPDU);
    ResponseEvent response = snmp.get(pdu, comtarget);

  if (response != null)
    {
      System.out.println("Got Response from Agent");
      PDU responsePDU = response.getResponse();

      if (responsePDU != null)
      {
        int errorStatus = responsePDU.getErrorStatus();
        int errorIndex = responsePDU.getErrorIndex();
        String errorStatusText = responsePDU.getErrorStatusText();

        if (errorStatus == PDU.noError)
        {
          System.out.println("Snmp Get Response = " + responsePDU.getVariableBindings());
        }
        else
        {
          System.out.println("Error: Request Failed");
          System.out.println("Error Status = " + errorStatus);
          System.out.println("Error Index = " + errorIndex);
          System.out.println("Error Status Text = " + errorStatusText);
        }
      }
      else
      {
        System.out.println("Error: Response PDU is null");
      }
    }
    else
    {
      System.out.println("Error: Agent Timeout... ");
    }
    snmp.close();
  }

输出:

Sending Request to Agent...
request : GET[requestID=1, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1.0 = Null]]
Got Response from Agent
Snmp Get Response = [1.3.6.1.2.1.1.1.0 = Dell Networking N3048, 6.0.1.3, Linux 3.6.5-320b2282]

0 个答案:

没有答案