package SNMP; import com.adventnet.snmp.snmp2.SnmpAPI; import com.adventnet.snmp.snmp2.SnmpException; import com.adventnet.snmp.snmp2.SnmpOID; import com.adventnet.snmp.snmp2.SnmpPDU; import com.adventnet.snmp.snmp2.SnmpSession; import com.adventnet.snmp.snmp2.SnmpVar; import com.adventnet.snmp.snmp2.SnmpVarBind; import java.util.Vector; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author max */ public class work { public static void main(String[] args) { work w = new work(); Vector resultList[] = new Vector[1]; Vector indexList = new Vector(); // SnmpOID oids[] = new SnmpOID[1]; // oids[0] = new SnmpOID(".1.3.6.1.4.1.10072.2.20.1.1.5.1.1.28.54001.1"); // readCurrentMib("192.168.2.242", "public", oids, resultList, indexList); // for(int i =0;i<indexList.size();i++) // { // System.out.println(indexList.get(i)); // System.out.println(resultList[0].get(i)); // } try { //写 w.setMib("192.168.2.242", 161, "public", "private", ".1.3.6.1.4.1.10072.2.20.1.1.5.1.1.28.54001.1", "4"); //读 SnmpOID oids[] = new SnmpOID[1]; oids[0] = new SnmpOID(".1.3.6.1.4.1.10072.2.20.1.1.5.1.1.28.54001.1"); readCurrentMib("192.168.2.242", "public", oids, resultList, indexList); System.out.println(indexList.get(0)); System.out.println(resultList[0].get(0)); } catch (SnmpException ex) { Logger.getLogger(work.class.getName()).log(Level.SEVERE, null, ex); } } //主机、端口、读团体字、写团体字、oid号、值 private void setMib(String host, int port, String community, String writeCommunity, String oid, String value) throws SnmpException { SnmpAPI snmpapi = new SnmpAPI(); snmpapi.start(); SnmpSession session = new SnmpSession(snmpapi); session.open(); SnmpPDU pdu = new SnmpPDU(); try { pdu.setRemoteHost(host); pdu.setRemotePort(port); pdu.setCommunity(community); pdu.setWriteCommunity(writeCommunity); pdu.setTimeout(5000); pdu.setRetries(3); pdu.setVersion(SnmpAPI.SNMP_VERSION_2C); pdu.setCommand(SnmpAPI.SET_REQ_MSG); SnmpOID snmpOID = new SnmpOID(oid); // add OID SnmpVar var = null; try { var = SnmpVar.createVariable(value, SnmpAPI.INTEGER); //设置下发配置的值和类型 } catch (SnmpException e) { System.err.println("Cannot create variable: " + oid + " with value:1"); return; } SnmpVarBind varbind = new SnmpVarBind(snmpOID, var); //oid 和 var绑定 pdu.addVariableBinding(varbind); // add variable binding // Send PDU try { pdu = session.syncSend(pdu); } catch (SnmpException e) { System.err.println("Sending PDU" + e.getMessage()); } if (pdu == null) { System.out.println("Request timed out!"); return; } System.out.println("Response PDU received from " + pdu.getAddress() + ", community: " + pdu.getCommunity()); if (pdu.getErrstat() != 0) { System.err.println(pdu.getError()); } else { System.out.println(pdu.printVarBinds()); } } catch (Exception ex) { ex.printStackTrace(); } finally { if (session != null) { snmpapi.close(); session.close(); session = null; } } } //只读当前的项 public static boolean readCurrentMib(String hostip,String community, SnmpOID oids[],Vector resultList[],Vector indexList) { SnmpAPI snmpapi = null; SnmpSession session = null; String host =hostip; try{ snmpapi = new SnmpAPI(); snmpapi.start(); session = new SnmpSession(snmpapi); session.open(); SnmpPDU pdu = new SnmpPDU(); pdu.setRemoteHost(host); pdu.setRemotePort(161); pdu.setCommunity(community); pdu.setTimeout(5000); pdu.setRetries(2); pdu.setVersion(SnmpAPI.SNMP_VERSION_2C); pdu.setCommand(SnmpAPI.GET_REQ_MSG); for(int i=0;i < oids.length;i++) { pdu.addNull(oids[i]); resultList[i] = new Vector(); do{ try{ try { Thread.sleep(10); } catch (Exception ex1) { } SnmpPDU v = session.syncSend(pdu); if(v!=null) { resultList[i].add(v.getVariable(0)); indexList.add(v.getObjectID(0)); break; } }catch (Exception e) { e.printStackTrace(); return false; } }while(true); } }catch (Exception ex) { ex.printStackTrace(); return false; } finally { if (null != session) { snmpapi.close(); session.close(); session = null; } } return true; } //读mib不能配到索引,因为它要全读回来。用的是GETNEXT_REQ_MSG方式。 public static boolean readNextMib(String hostip, String community, SnmpOID oids[], Vector resultList[], Vector indexList) { SnmpAPI snmpapi = null; SnmpSession session = null; String host = hostip; try { snmpapi = new SnmpAPI(); snmpapi.start(); session = new SnmpSession(snmpapi); session.open(); com.adventnet.snmp.snmp2.SnmpPDU pdu = new SnmpPDU(); pdu.setRemoteHost(host); pdu.setRemotePort(161); pdu.setCommunity(community); pdu.setTimeout(5000); pdu.setRetries(2); pdu.setVersion(SnmpAPI.SNMP_VERSION_2C); pdu.setCommand(SnmpAPI.GETNEXT_REQ_MSG); for (int i = 0; i < oids.length; i++) { //多个参数 pdu.addNull(oids[i]); resultList[i] = new Vector(); do { //一个参数 try { try { Thread.sleep(10); } catch (Exception ex1) { } SnmpPDU v = session.syncSend(pdu); //读取下一个ID号,54001.1 if (null != v) { if (isInSubTree(oids[i].toIntArray(), v.getObjectID(0))) { //保证是读的是同一个参数 resultList[i].add(v.getVariable(0).toString()); //添加值,这里的0是因为只有一个变量 pdu.removeVariableBinding(0); //去掉原先的".1.3.6.1.4.1.10072.2.20.1.1.5.1.1.28" pdu.addNull(v.getObjectID(0)); //变成现在的".1.3.6.1.4.1.10072.2.20.1.1.5.1.1.28。54001.1" // i==0 和 i==1 、i==2…… 时,都是一样的,所以取一遍就可以了 if (i == 0) { //只记录第一个参数的索引ID String d = getIndexs(oids[i].toIntArray(), v.getObjectID(0)); //添加索引:添加v多于oid的那一部分 indexList.add(d); } } else { break; //这就跳出循环了 } } else { return false; } } catch (Exception e) { e.printStackTrace(); return false; } } while (true); pdu.removeVariableBinding(0); //0是因为只有一个变量 } } catch (Exception ex) { ex.printStackTrace(); return false; } finally { if (null != session) { snmpapi.close(); session.close(); session = null; } } return true; } //块读 public static boolean readBulkMib(String hostip, String community, SnmpOID oids[], Vector resultList[], Vector indexList) { SnmpAPI snmpapi = null; SnmpSession session = null; String host = hostip; int nonRepInt = 0; int maxRepInt = 4; try { snmpapi = new SnmpAPI(); snmpapi.start(); session = new SnmpSession(snmpapi); session.open(); com.adventnet.snmp.snmp2.SnmpPDU pdu = new SnmpPDU(); pdu.setRemoteHost(host); pdu.setRemotePort(161); pdu.setCommunity(community); pdu.setTimeout(5000); pdu.setRetries(2); pdu.setVersion(SnmpAPI.SNMP_VERSION_2C); pdu.setCommand(SnmpAPI.GETBULK_REQ_MSG); pdu.setMaxRepetitions(maxRepInt); pdu.setNonRepeaters(nonRepInt); for (int i = 0; i < oids.length; i++) { pdu.addNull(oids[i]); resultList[i] = new Vector(); do { try { try { Thread.sleep(10); } catch (Exception ex1) { } SnmpPDU v = session.syncSend(pdu); //块读时,一次读回maxRepInt个变量 if (null != v) { int j = 0; SnmpOID loid = null; for (j = 0; j < maxRepInt; j++) { if (isInSubTree(oids[i].toIntArray(), v.getObjectID(j))) { resultList[i].add(v.getVariable(j).toString()); pdu.removeVariableBinding(j); loid = v.getObjectID(j); String d = getIndexs(oids[i].toIntArray(), v.getObjectID(j)); indexList.add(d); } else { break; } } if (j == maxRepInt) { pdu.addNull(loid); //读到第四行后,把第四行的id放进去,接着读 } else { break; } } else { return false; } } catch (Exception e) { e.printStackTrace(); return false; } } while (true); pdu.removeVariableBinding(0); } } catch (Exception ex) { ex.printStackTrace(); return false; } finally { if (null != session) { snmpapi.close(); session.close(); session = null; } } return true; } static boolean isInSubTree(int oids[], SnmpOID snmpoid) { //oid设定的oid值,snmpoid为返回来读取的oid值 if (snmpoid == null) { return false; } int ai1[] = (int[]) snmpoid.toValue(); if (ai1 == null) { return false; } int i = oids.length; if (ai1.length < i) { return false; } for (int j = i - 1; j >= 0; j--) { //保证读回来的前面.1.3.6.1.4.1.10072.2.20.1.1.5.1.1.28是一样的 if (ai1[j] != oids[j]) { return false; } } return true; } static String getIndexs(int oids[], SnmpOID snmpoid) { String ret = ""; if (snmpoid == null) { return ""; } int ai1[] = (int[]) snmpoid.toValue(); if (ai1 == null) { return ""; } int i = oids.length; if (ai1.length < i) { return ""; } for (int j = i - 1; j >= 0; j--) { if (ai1[j] != oids[j]) { return ""; } } for (int k = i; k < ai1.length; k++) { ret = ret + ai1[k]; ret = ret + "."; } return ret; } }