Merhaba,
Projemin bir ayağında SNMP ile rooter’dan veri almam gerekiyordu.
Bu işlemi aşağıdaki kod bloğu ile yaptım.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
private String convertData(String value) throws UnsupportedEncodingException{ String returnValue = ""; // buradaki value değeri istediğimiz bilginin mibidir. // ben host name için "1.3.6.1.2.1.1.4.0" değerini yolladım. try { Snmpmgr snmpmgr1 = new Snmpmgr(); snmpmgr1.setRuntimeLicense("Buraya lisans kodunu eklemeniz gerekiyor."); snmpmgr1.setSNMPVersion(Snmpmgr.snmpverV2c); snmpmgr1.setRemoteHost("Buraya rooter'ın ip'sini yazmalısınız."); byte[] sValue; String sOID = value; snmpmgr1.getObjects().clear(); SNMPObject so = new SNMPObject(sOID); snmpmgr1.getObjects().add(so); try { snmpmgr1.sendGetNextRequest(); } catch (IPWorksSSNMPException ex) { System.out.println(" xxx "+ex); } SNMPObjectList ol = snmpmgr1.getObjects(); sOID = ol.item(0).getOid(); sValue = ol.item(0).getValue(); String sType = ol.item(0).getTypeString(); if (snmpmgr1.getErrorIndex() == 0) { if (sType.equals("OctetString")) { String getMyValue = new String(sValue); returnValue = getMyValue; } else if (sType.equals("TimeTicks")) { String getMyValue = new String(sValue); returnValue = getMyValue; } else if (sType.equals("IpAddress")) { String getMyValue = new String(sValue); returnValue = getMyValue; } else if (sType.equals("ObjectId")) { String getMyValue = new String(sValue); returnValue = getMyValue; } else if (sType.equals("Integer")) { String getMyValue = new String(sValue); returnValue = getMyValue; } else if (sType.equals("Gauge32")) { String getMyValue = new String(sValue); returnValue = getMyValue; } else if (sType.equals("Counter32")) { String getMyValue = new String(sValue); returnValue = getMyValue; } else { System.err.println("****id :" + so.getOid() + " value : " + sValue + " Tipi :" + sType); } } } catch (Exception e) { e.printStackTrace(); System.out.println("**** Exception "); } return returnValue; } |
Kolay gelsin…
Her hangi bir durumda mail ile sorabilirsiniz…
Bekir