目录:
1、java通过snmp4j实现 set、get、getNext,getBulk
2、java调用snmp4j获取snmp表格所有元素
1、java调用snmp4j实现 set、get、getNext,getBulk
package com.harara.test; import org.snmp4j.CommunityTarget; import org.snmp4j.PDU; import org.snmp4j.Snmp; import org.snmp4j.TransportMapping; import org.snmp4j.event.ResponseEvent; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.smi.OID; import org.snmp4j.smi.OctetString; import org.snmp4j.smi.UdpAddress; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultUdpTransportMapping; import java.io.IOException; import java.util.Vector; /** * snmp版本为v2c和v1的调用方式一样,这里以v2c为例 * @author : harara * @version : 2.0 * @date : 2020/6/28 17:22 */ public class SNMPv2Test { private static CommunityTarget target = null; public static void main(String[] args) { try { //Set testSnmpSet(); //Get testSnmpGet(); //getNext testSnmpGetNext(); //getBulk testSnmpGetBulk(); } catch (Exception e) { e.printStackTrace(); } } /** * 测试set报文 */ public static void testSnmpSet() throws Exception { //1、初始化snmp SNMPv2Test snmPv2Test = new SNMPv2Test(); Snmp snmp = snmPv2Test.initSnmp(); //2.创建CommunityTarget CommunityTarget communityTarget = snmPv2Test.getTarget(); //3.设置udp PDU pdu = new PDU(); pdu.setType(PDU.SET); pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new OctetString("test"))); //4.发送请求,解析响应内容 snmPv2Test.sendSnmpRequest(snmp, pdu, communityTarget); } /** * 测试get报文 */ public static void testSnmpGet() throws Exception{ //1、初始化snmp SNMPv2Test snmPv2Test = new SNMPv2Test(); Snmp snmp = snmPv2Test.initSnmp(); //2.创建CommunityTarget CommunityTarget communityTarget = snmPv2Test.getTarget(); //3.设置udp PDU pdu = new PDU(); pdu.setType(PDU.GET); pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"))); //4.发送请求,解析响应内容 snmPv2Test.sendSnmpRequest(snmp,pdu,communityTarget); } /** * 测试getBulk */ public static void testSnmpGetBulk() throws Exception{ //1、初始化snmp SNMPv2Test snmPv2Test = new SNMPv2Test(); Snmp snmp = snmPv2Test.initSnmp(); //2.创建CommunityTarget CommunityTarget communityTarget = snmPv2Test.getTarget(); //3.设置udp PDU pdu = new PDU(); pdu.setType(PDU.GETBULK); //non-repeaters字段指出只返回一个后继变量的变量数。max-repetitions字段指出其他的变量应返回的最大的后继变量数 pdu.setMaxRepetitions(7); //must set it, default is 0 pdu.setNonRepeaters(0); pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"))); //4.发送请求,解析响应内容 snmPv2Test.sendSnmpRequest(snmp, pdu, communityTarget); } /** * 测试getNext报文 */ public static void testSnmpGetNext() throws Exception{ //1、初始化snmp SNMPv2Test snmPv2Test = new SNMPv2Test(); Snmp snmp = snmPv2Test.initSnmp(); //2.创建CommunityTarget CommunityTarget communityTarget = snmPv2Test.getTarget(); //3.设置udp PDU pdu = new PDU(); pdu.setType(PDU.GETNEXT); pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"))); //4.发送请求,解析响应内容 snmPv2Test.sendSnmpRequest(snmp, pdu, communityTarget); } /** * 发送snmp请求并解析响应内容 * @param snmp * @param pdu * @param target * @throws Exception */ public void sendSnmpRequest(Snmp snmp, PDU pdu, CommunityTarget target) throws Exception{ //1.发送请求 ResponseEvent responseEvent = snmp.send(pdu, target); //2.解析响应内容 PDU response = responseEvent.getResponse(); if (response == null) { System.out.println("TimeOut..."); } else { if (response.getErrorStatus() == PDU.noError) { Vector<? extends VariableBinding> vbs = response.getVariableBindings(); for (VariableBinding vb : vbs) { System.out.println(vb + " ," + vb.getVariable().getSyntaxString()); } } else { System.out.println("Error:" + response.getErrorStatusText()); } } } /** * 初始化snmp * @return */ public Snmp initSnmp(){ TransportMapping transport = null; try { transport = new DefaultUdpTransportMapping(); Snmp snmp = new Snmp(transport); snmp.listen(); //transport.listen(); return snmp; } catch (IOException e) { e.printStackTrace(); return null; } } /** * 创建/获取CommunityTarget * @return */ public CommunityTarget getTarget(){ if(target == null){ synchronized (this) { if(target == null){ //创建CommunityTarget target = new CommunityTarget(); //团体名字 target.setCommunity(new OctetString("public")); //snmp服务的地址 target.setAddress(new UdpAddress("192.169.1.66/161")); //失败重试次数 target.setRetries(1); //超时时间 单位毫秒 target.setTimeout(3000); //snmp通信协议版本 v1,v2c调用方式一样,这里以v2为例 target.setVersion(SnmpConstants.version2c); } } } return target; } }
2、java调用snmp4j获取snmp表格所有元素
package com.chenly.test; import org.snmp4j.CommunityTarget; import org.snmp4j.PDU; import org.snmp4j.Snmp; import org.snmp4j.TransportMapping; import org.snmp4j.event.ResponseEvent; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.smi.*; import org.snmp4j.transport.DefaultUdpTransportMapping; import org.snmp4j.util.DefaultPDUFactory; import org.snmp4j.util.TableEvent; import org.snmp4j.util.TableUtils; import java.io.IOException; import java.util.List; /** * @author : harara * @version : 2.0 * @date : 2020/9/3 15:41 */ public class SNMPTableTest { private static CommunityTarget target = null; public static void main(String[] args) { testTableUtils(); testTableByGetNext(); } /** * 使用TableUtils方式获取表格的所有元素 */ public static void testTableUtils(){ SNMPTableTest snmpTableTest = new SNMPTableTest(); Snmp snmp = snmpTableTest.initSnmp(); CommunityTarget communityTarget = snmpTableTest.getTarget(); //GETNEXT or GETBULK TableUtils utils = new TableUtils(snmp, new DefaultPDUFactory(PDU.GETBULK)); //only for GETBULK, set max-repetitions, default is 10 utils.setMaxNumRowsPerPDU(2); OID[] columnOids = new OID[] { new OID("1.3.6.1.2.1.4.20") }; // If not null, all returned rows have an index in a range (lowerBoundIndex, upperBoundIndex] //第3个参数是最小列oid索引,第四个参数是最大列oid索引 List<TableEvent> list = utils.getTable(communityTarget, columnOids, new OID("1"), new OID("10")); if(list == null || list.size() <= 0){ System.out.println("list为空"); return; } System.out.println("list size : "+ list.size()); for (TableEvent event : list) { System.out.println(event); } } /** * 通过getNext遍历获取表格所有元素 * @return */ public static void testTableByGetNext() { SNMPTableTest snmpTableTest = new SNMPTableTest(); Snmp snmp = null; CommunityTarget communityTarget = null; PDU pdu = null; OID targetOID = new OID("1.3.6.1.2.1.4.20"); try{ snmp = snmpTableTest.initSnmp(); communityTarget = snmpTableTest.getTarget(); pdu = new PDU(); pdu.add(new VariableBinding(targetOID)); boolean finished = false; //walk操作 while (!finished) { VariableBinding vb = null; // 向Agent发送PDU实施getNext操作,并接收Response ResponseEvent respEvent = snmp.getNext(pdu, target); // 解析Response数据 PDU response = respEvent.getResponse(); if (null == response) { finished = true; break; } vb = response.get(0); // 检查是否结束 finished = checkWalkFinished(targetOID, pdu, vb); if (!finished) { System.out.println(vb.getOid() + "="+ vb.getVariable() +" ," + vb.getVariable().getSyntaxString()); // 将variable binding设置到下一个 pdu.setRequestID(new Integer32(0)); pdu.set(0, vb); } } }catch(Exception e){ e.printStackTrace(); }finally{ if(snmp !=null){ try { snmp.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 检查walk是否结束 * @param targetOID * @param pdu * @param vb * @return */ private static boolean checkWalkFinished(OID targetOID, PDU pdu, VariableBinding vb) { boolean finished = false; if (pdu.getErrorStatus() != 0) { finished = true; } else if (vb.getOid() == null) { finished = true; } else if (vb.getOid().size() < targetOID.size()) { finished = true; } else if (targetOID.leftMostCompare(targetOID.size(), vb.getOid()) != 0) { finished = true; } else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) { finished = true; } else if (vb.getOid().compareTo(targetOID) <= 0) { finished = true; } return finished; } /** * 初始化snmp * @return */ public Snmp initSnmp(){ TransportMapping transport = null; try { transport = new DefaultUdpTransportMapping(); Snmp snmp = new Snmp(transport); snmp.listen(); //transport.listen(); return snmp; } catch (IOException e) { e.printStackTrace(); return null; } } /** * 创建/获取CommunityTarget * @return */ public CommunityTarget getTarget(){ if(target == null){ synchronized (this) { if(target == null){ //创建CommunityTarget target = new CommunityTarget(); //团体名字 target.setCommunity(new OctetString("public")); //snmp服务的地址 target.setAddress(new UdpAddress("192.169.1.66/161")); //失败重试次数 target.setRetries(1); //超时时间 单位毫秒 target.setTimeout(3000); //snmp通信协议版本 v1,v2c调用方式一样,这里以v2为例 target.setVersion(SnmpConstants.version2c); } } } return target; } }
参考地址: