public void snmpWalk(String oidFrom) throws SnmpException { PDU request = new PDU(); request.setType(PDU.GETNEXT); request.add(new VariableBinding(new OID(oidFrom))); request.setNonRepeaters(0); OID rootOID = request.get(0).getOid(); PDU response = null; int objects = 0; int requests = 0; long startTime = System.currentTimeMillis(); try { Snmp snmp = new Snmp(new DefaultUdpTransportMapping()); snmp.listen(); m_sessionDestroyed = false; do { requests++; ResponseEvent responseEvent = snmp.send(request, getTarget(getReadCommunity())); response = responseEvent.getResponse(); if (response != null) { objects += response.size(); } } while (!processWalk(response, request, rootOID) && !m_sessionDestroyed); } catch (SnmpException e) { throw e; } catch (Exception e) { e.printStackTrace(); throw new SnmpException(e.getMessage()); } if (m_snmpResponseHandler != null) { m_snmpResponseHandler.requestStats(requests, objects, System.currentTimeMillis() - startTime); } }
/** * @param ip * @param community */ public static void snmpGetList(String ip, String community, List<String> oidList) { CommunityTarget target = SnmpUtil.createDefault(ip, community); Snmp snmp = null; try { PDU pdu = new PDU(); for (String oid : oidList) { pdu.add(new VariableBinding(new OID(oid))); } DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping(); transport.listen(); snmp = new Snmp(transport); System.out.println("------->发送消息<-------"); pdu.setType(PDU.GET); ResponseEvent respEvent = snmp.send(pdu, target); System.out.println("PeerAddress:" + respEvent.getPeerAddress()); PDU response = respEvent.getResponse(); if (response == null) { System.out.println("response is null, request time out"); } else { System.out.println("response pdu size is " + response.size()); for (int i = 0; i < response.size(); i++) { VariableBinding vb = response.get(i); System.out.println(vb.getOid() + " = " + vb.getVariable()); } } System.out.println("SNMP GET List OID value finished !"); } catch (Exception e) { e.printStackTrace(); System.out.println("SNMP GetList Exception:" + e); } finally { if (snmp != null) { try { snmp.close(); } catch (IOException ex1) { snmp = null; } } } }
private boolean processWalk(PDU response, PDU request, OID rootOID) throws SnmpException { if ((response == null) || (response.getErrorStatus() != 0) || (response.getType() == PDU.REPORT)) { return true; } boolean finished = false; OID lastOID = request.get(0).getOid(); for (int i = 0; (!finished) && (i < response.size()); i++) { VariableBinding vb = response.get(i); if ((vb.getOid() == null) || (vb.getOid().size() < rootOID.size()) || (rootOID.leftMostCompare(rootOID.size(), vb.getOid()) != 0)) { finished = true; } else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) { outputResponse(vb); finished = true; } else if (vb.getOid().compareTo(lastOID) <= 0) { throw new SnmpException( "Variable received is not lexicographic successor of requested one:" + vb.toString() + " <= " + lastOID); } else { outputResponse(vb); lastOID = vb.getOid(); } } if (response.size() == 0) { finished = true; } if (!finished) { VariableBinding next = response.get(response.size() - 1); next.setVariable(new Null()); request.set(0, next); request.setRequestID(new Integer32(0)); } return finished; }
@Override public void onResponse(ResponseEvent event) { SnmpUriResponse response = new SnmpUriResponse(SnmpUriResponse.Type.TIMEOUT); PDU responsePDU = event.getResponse(); if (responsePDU != null) { if (responsePDU.getErrorStatus() != PDU.noError) { response = new SnmpUriResponse(responsePDU.getErrorStatus()); } else { response = new SnmpUriResponse( Collections.singletonList( responsePDU .getVariableBindings() .toArray(new VariableBinding[responsePDU.size()]))); } } callback.onResponse(response, url, event.getUserObject()); }
public SnmpVars(PDU data) { super(data.size()); join(data); }
public void join(PDU data) { for (int i = 0; i < data.size(); i++) { VariableBinding vb = data.get(i); addVariable(vb); } }