为什么responseEvent.getResponse()为null?

时间:2018-01-16 16:25:13

标签: java snmp

我想学习并测试SNMP。在我的应用程序中,我想测试snmp从windows 10 pc到ubuntu pc获取一些细节。 运行应用程序时出错:

  

responseEvent.getResponse()为空


请帮忙。

这是我的代码:

   public static void main(String[] args) {
    try {
        OctetString community1 = new OctetString("public");
        Address targetaddress = new UdpAddress("127.0.0.1/161");
        TransportMapping transportMapping = new DefaultUdpTransportMapping();
        transportMapping.listen();
        CommunityTarget comtarget = new CommunityTarget();
        comtarget.setCommunity(community1);
        comtarget.setVersion(SnmpConstants.version1);
        comtarget.setAddress(targetaddress);
        comtarget.setRetries(2);
        comtarget.setTimeout(10000);
        PDU pdu = new PDU();
        pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.1.0")));
        pdu.setType(PDU.GET);
        Snmp snmp = new Snmp(transportMapping);

        ResponseEvent responseEvent = snmp.get(pdu, comtarget);
        String str = "";
        if (responseEvent != null && responseEvent.getResponse().getErrorStatusText().equalsIgnoreCase("Success")) {
            PDU pduresponse = responseEvent.getResponse();
            str = pduresponse.getVariableBindings().firstElement().toString();
            if (str.contains("=")) {
                int len = str.indexOf("=");
                str = str.substring(len + 1, str.length());
            }
        } else {
            System.out.println("responseEvent or responseEvent.getResponse() == null.  Feeling like a TimeOut occured ");
        }
        snmp.close();
        System.out.println(str);
    } catch (Exception e) {
    }
 }

1 个答案:

答案 0 :(得分:0)

  

...在我的应用程序中,我想测试从Windows 10 pc到的snmp   ubuntu pc获取一些细节...

您可能希望使用其他targetaddress,因为" 127.0.0.1/161"您将该请求发送到localhost。我猜测127.0.0.1应该替换为您查询的PC的IP地址......

其他PC当然必须在端口161 / UDP上运行某种SNMP守护进程,该端口被配置为响应请求。