[SNMP4J] Need help:Unable to run sample snmp code with OIDs
Hi, I have tried running code by commenting hte line u have asked but error is still coming ERROR: Exception in thread "DefaultUDPTransportMapping_192.168.2.2/0" java.lang.NullPointerException at org.snmp4j.transport.DefaultUdpTransportMapping$ListenThread.run(Unknown Source) at java.lang.Thread.run(Unknown Source) at org.snmp4j.util.DefaultThreadFactory$WorkerThread.run(Unknown Source) java.lang.NullPointerException at SNMP4JHelper.snmpGet(SNMP4JHelper.java:132) at SNMP4JHelper.main(SNMP4JHelper.java:45)Response= ___ SNMP4J mailing list SNMP4J@agentpp.org http://lists.agentpp.org/mailman/listinfo/snmp4j
[SNMP4J] Need help:Unable to run sample snmp code with OIDs
Hi, I have one more code the output is coming NULL for it i dont why.Please if u can help me on this and the output language is not understandable import java.io.IOException; import org.snmp4j.*; import org.snmp4j.smi.Address; import org.snmp4j.CommandResponder; import org.snmp4j.CommandResponderEvent; import org.snmp4j.CommunityTarget; import org.snmp4j.PDU; import org.snmp4j.ScopedPDU; import org.snmp4j.Snmp; import org.snmp4j.TransportMapping; import org.snmp4j.UserTarget; import org.snmp4j.event.ResponseEvent; import org.snmp4j.event.ResponseListener; import org.snmp4j.mp.MPv3; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.security.AuthMD5; import org.snmp4j.security.PrivDES; import org.snmp4j.security.SecurityLevel; import org.snmp4j.security.SecurityModels; import org.snmp4j.security.SecurityProtocols; import org.snmp4j.security.USM; import org.snmp4j.security.UsmUser; import org.snmp4j.smi.Address; import org.snmp4j.smi.GenericAddress; import org.snmp4j.smi.OID; import org.snmp4j.smi.OctetString; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultUdpTransportMapping; import org.snmp4j.Snmp; import org.snmp4j.CommunityTarget; public class Main { public static void main(String[] args) throws IOException{ // TODO code application logic here Address targetAddress = GenericAddress.parse("udp:127.0.0.1/161"); TransportMapping transport = new DefaultUdpTransportMapping(); Snmp snmp = new Snmp(transport); //USM - User Based Security Model USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0); SecurityModels.getInstance().addSecurityModel(usm); transport.getListenAddress(); if(transport.getListenAddress()!=null) System.out.println("Adresa de ascultare este:"+transport.getListenAddress()); else System.out.println("Nu exista"); byte a[]=new byte[]{1,2}; transport.sendMessage(targetAddress, a); transport.listen(); if(transport.isListening()) System.out.println("Marimea maxima a mesajul este: "+transport.getMaxInboundMessageSize()); System.out.println(snmp.getNextRequestID()); snmp.getUSM().addUser(new OctetString("MD5DES"), new UsmUser(new OctetString("MD5DES"), AuthMD5.ID, new OctetString("MD5DESUserAuthPassword"), PrivDES.ID, new OctetString("MD5DESUserPrivPassword"))); // creare target UserTarget target = new UserTarget(); target.setAddress(targetAddress); target.setRetries(1); target.setTimeout(5000); target.setVersion(SnmpConstants.version3); target.setSecurityLevel(SecurityLevel.AUTH_PRIV); target.setSecurityName(new OctetString("MD5DES")); System.out.println("User~ul: "+snmp.getUSM()); System.out.println("Adresa la care va fi trimis mesajul: "+target.getAddress()); // creare PDU PDU pdu = new ScopedPDU(); pdu.add(new VariableBinding(new OID("1.3.6"))); pdu.setType(PDU.GETNEXT); System.out.println("Tip PDU: "+pdu.getType()); // trimitere PDU ResponseEvent response = snmp.send(pdu, target); // extrage raspunsul PDU (poate fi null daca timpul expira) PDU responsePDU = response.getResponse(); System.out.println("Raspuns: "+response.getResponse()); //extrage adresa folosita de agent pentru a trimite raspunsul Address peerAddress = response.getPeerAddress(); System.out.println(response.getRequest()); snmp.listen(); CommunityTarget communityTarget = new CommunityTarget(); communityTarget.setCommunity(new OctetString("public")); communityTarget.setAddress(targetAddress); communityTarget.setRetries(3); communityTarget.setTimeout(3000); communityTarget.setVersion(SnmpConstants.version1); System.out.println("Comunity target: "+communityTarget.getCommunity()); // creare PDU pdu = new PDU(); pdu.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,1,1}))); pdu.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,1,2}))); pdu.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,1,3}))); pdu.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,1,4}))); pdu.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,1,5}))); pdu.setType(PDU.GETNEXT); snmp.listen(); ResponseListener listener = new ResponseListener() { public void onResponse(ResponseEvent event) { PDU response = event.getResponse(); PDU requ
[SNMP4J] Need help:Unable to run sample snmp code with OIDs
Hi, I am sending the code with the error line no. //import java.net.InetAddress; import org.snmp4j.CommunityTarget; import org.snmp4j.PDU; import org.snmp4j.Snmp; import org.snmp4j.TransportMapping; import org.snmp4j.event.ResponseEvent; import org.snmp4j.event.ResponseListener; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.smi.*; import org.snmp4j.transport.DefaultUdpTransportMapping; //import java.lang.NullPointerException; public class SNMP4JHelper { public static final int SNMP_PORT = 161; public static final String READ_COMMUNITY = "public"; public static final String WRITE_COMMUNITY= "private"; public static final int mSNMPVersion =0; // 0 represents SNMP version=1 public static final String OID_Hard_disk = "1.3.6.1.2.1.1.3.0"; // "1.3.6.1.4.1.2021.9.1.7.1"; // ".1.3.6.1.4.1.2021.4.4.0"; // "1.3.6.1.4.1.311.1.1.3.1.1.5.1.3"; //"1.3.6.1.4.1.318.1.1.1.12.3.2.1.3.1"; //public static final String OID_UPS_BATTERY_CAPACITY="1.3.6.1.4.1.318.1.1.1.2.2.1.0"; public static void main(String[] args) { try { // "172.20.1.150" String strIPAddress = "127.0.0.1"; SNMP4JHelper objSNMP = new SNMP4JHelper(); //objSNMP.snmpSet(); int Value = 1; /// //Set Value=2 to trun OFF UPS OUTLET Group1 //Value=1 to trun ON UPS OUTLET Group1 // // "strIPAddress, WRITE_COMMUNITY,OID_Hard_disk, Value" // UPS_OUTLET_GROUP1 objSNMP.snmpSet(strIPAddress, WRITE_COMMUNITY,OID_Hard_disk, Value); // //Get Basic state of UPS / //line 45// String batteryCap =objSNMP.snmpGet(strIPAddress,READ_COMMUNITY,OID_Hard_disk); } catch (Exception e) { e.printStackTrace(); } } /* * The following code valid only SNMP version1. This * method is very useful to set a parameter on remote device. * */ public void snmpSet(String strAddress, String community, String strOID, int Value) { strAddress= strAddress+"/"+SNMP_PORT; Address targetAddress = GenericAddress.parse(strAddress); Snmp snmp; try { TransportMapping transport = new DefaultUdpTransportMapping(); snmp = new Snmp(transport); transport.listen(); CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString(community)); target.setAddress(targetAddress); target.setRetries(2); target.setTimeout(5000); target.setVersion(SnmpConstants.version1); PDU pdu = new PDU(); // pdu.add(new VariableBinding(new OID(strOID), new Integer32(Value))); pdu.setType(PDU.SET); ResponseListener listener = new ResponseListener() { public void onResponse(ResponseEvent event) { // Always cancel async request when response has been received // otherwise a memory leak is created! Not canceling a request // immediately can be useful when sending a request to a broadcast // address ((Snmp)event.getSource()).cancel(event.getRequest(), this); if ( event.getResponse() != null ) { System.out.println("Set Status is:"+event.getResponse().getErrorStatusText()); } } }; snmp.send(pdu, target, null, listener); snmp.close(); } catch (Exception e) { e.printStackTrace(); } } /* * The code is valid only SNMP version1. SnmpGet method * return Response for given OID from the Device. */ public String snmpGet(String strAddress, String community, String strOID) { String str=""; try { OctetString comm1 = new OctetString(community); strAddress= strAddress+"/"+ SNMP_PORT; Address targetaddress = new UdpAddress(strAddress); TransportMapping transport = new DefaultUdpTransportMapping(); transport.listen(); CommunityTarget comtarget = new CommunityTarget(); comtarget.setCommunity(comm1); comtarget.setVersion(SnmpConstants.version1); comtarget.setAddress(targetaddress); comtarget.setRetries(2); comtarget.setTimeout(5000); PDU pdu = new PDU(); ResponseEvent response; Snmp snmp; pdu.add(new VariableBinding(new OID(strO