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); } }
void snmpSetValue(String oid, int syntax, String value) throws SnmpException { VariableBinding varbind = getVarBindForSetRequest(oid, syntax, value); PDU request = new PDU(); request.setType(PDU.SET); request.add(varbind); PDU response = null; long startTime = System.currentTimeMillis(); try { Snmp snmp = new Snmp(new DefaultUdpTransportMapping()); snmp.listen(); ResponseEvent responseEvent = snmp.send(request, getTarget(getWriteCommunity())); response = responseEvent.getResponse(); // System.out.println(response); } catch (Exception e) { e.printStackTrace(); throw new SnmpException(e.getMessage()); } }
private void createSession() throws IOException { if (snmp == null) { Address address = GenericAddress.parse(deviceAddress); AbstractTransportMapping transport; if (address instanceof TlsAddress) { transport = new TLSTM(); } else if (address instanceof TcpAddress) { transport = new DefaultTcpTransportMapping(); } else { transport = new DefaultUdpTransportMapping(); } // Could save some CPU cycles: transport.setAsyncMsgProcessingSupported(false); snmp = new Snmp(transport); snmp.listen(); if (getSnmpVersion() == SnmpConstants.version3) { snmp.getUSM() .addUser( new OctetString("user"), new UsmUser( new OctetString("user"), AuthMD5.ID, new OctetString("user"), AuthMD5.ID, null)); // create the target target = new UserTarget(); target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV); target.setSecurityName(new OctetString("user")); } else { // create the target target = new CommunityTarget(); ((CommunityTarget) target).setCommunity(new OctetString(configuration.getReadCommunity())); } target.setAddress(address); target.setVersion(getSnmpVersion()); target.setRetries(retries); target.setTimeout(timeout); } }