使用Java進行SNMP程式設計-SNMP4J-SNMPv1/v2-程式碼例項
阿新 • • 發佈:2018-12-12
使用SNMP4J可以很方便的實現一個SNMP NMS的功能。 對於SNMPv1/SNMPv2,簡單的說,只需要以下幾個步驟 1) 建立Snmp物件snmp 2) 建立CommunityTarget物件target,並指定community, version, address, timeout, retry等引數。 3) 建立PDU物件pdu,並指定操作型別(GET/GETNEXT/GETBULK/SET), 新增VariableBinding(也就是待操作的OID), 如果是GETBULK操作,還可以指定MaxRepetitions和NonRepeaters。 注意一定要指定MaxRepetitions,預設值是0,那樣不會返回任何結果。 4) 呼叫snmp.send(pdu, target)方法,傳送請求請返回結果。 此外,SNMP4J提供了TableUtils類,可以很輕鬆的實現一個SNMP Walk的功能,比如一次返回一個表格下面的所有MIB值。 TableUtils類還支援指定ower bound index 和 upper bound index, 得到更小範圍的結果以提高效能。這一點對於從一個很多行的表中取一部分行的資料來說很重要。 Snmp還支援非同步的send方法。 下面的程式碼例項實現了GET, GETNEXT, GETBULK, Walk, SET操作, 以及一個利用非同步send方法實現的agent discovery: 對一個廣播地址傳送請求,等待agent返回的應答。 如果需要對一個範圍內的IP進行掃描,可以稍加改進,反覆呼叫非同步send多次傳送給不同IP。
import java.io.IOException; import java.util.List; import java.util.Vector; import org.snmp4j.CommunityTarget; import org.snmp4j.PDU; import org.snmp4j.Snmp; import org.snmp4j.event.ResponseEvent; import org.snmp4j.event.ResponseListener; 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 org.snmp4j.util.DefaultPDUFactory; import org.snmp4j.util.TableEvent; import org.snmp4j.util.TableUtils; public class SNMPv2Test { public static void main(String[] args) throws IOException, InterruptedException { Snmp snmp = new Snmp(new DefaultUdpTransportMapping()); snmp.listen(); CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString("public")); target.setVersion(SnmpConstants.version2c); target.setAddress(new UdpAddress("192.168.0.100/161")); target.setTimeout(3000); //3s target.setRetries(1); sendRequest(snmp, createGetPdu(), target); sendRequest(snmp, createGetNextPdu(), target); sendRequest(snmp, createGetBulkPdu(), target); snmpWalk(snmp, target); target.setCommunity(new OctetString("private")); sendRequest(snmp, createSetPdu(), target); CommunityTarget broadcastTarget = new CommunityTarget(); broadcastTarget.setCommunity(new OctetString("public")); broadcastTarget.setVersion(SnmpConstants.version2c); broadcastTarget.setAddress(new UdpAddress("192.168.0.255/161")); broadcastTarget.setTimeout(5000); //5s sendAsyncRequest(snmp, createGetNextPdu(), broadcastTarget); Thread.sleep(6000); //main thread wait 6s for the completion of asynchronous request } private static PDU createGetPdu() { PDU pdu = new PDU(); pdu.setType(PDU.GET); pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.3.0"))); //sysUpTime pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"))); //sysName pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5"))); //expect an no_such_instance error return pdu; } private static PDU createGetNextPdu() { PDU pdu = new PDU(); pdu.setType(PDU.GETNEXT); pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.3"))); //sysUpTime pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5"))); //sysName return pdu; } private static PDU createGetBulkPdu() { PDU pdu = new PDU(); pdu.setType(PDU.GETBULK); pdu.setMaxRepetitions(10); //must set it, default is 0 pdu.setNonRepeaters(0); pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1"))); //system return pdu; } private static PDU createSetPdu() { 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("sysname"))); //sysName return pdu; } private static void sendRequest(Snmp snmp, PDU pdu, CommunityTarget target) throws IOException { ResponseEvent responseEvent = snmp.send(pdu, target); 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()); } } } private static void sendAsyncRequest(Snmp snmp, PDU pdu, CommunityTarget target) throws IOException { snmp.send(pdu, target, null, new ResponseListener(){ @Override public void onResponse(ResponseEvent event) { PDU response = event.getResponse(); System.out.println("Got response from " + event.getPeerAddress()); 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()); } } }}); } private static void snmpWalk(Snmp snmp, CommunityTarget target) { TableUtils utils = new TableUtils(snmp, new DefaultPDUFactory(PDU.GETBULK));//GETNEXT or GETBULK utils.setMaxNumRowsPerPDU(5); //only for GETBULK, set max-repetitions, default is 10 OID[] columnOids = new OID[] { new OID("1.3.6.1.2.1.1.9.1.2"), //sysORID new OID("1.3.6.1.2.1.1.9.1.3"), //sysORDescr new OID("1.3.6.1.2.1.1.9.1.5") //wrong OID, expect an null in in VariableBinding array }; // If not null, all returned rows have an index in a range (lowerBoundIndex, upperBoundIndex] List<TableEvent> l = utils.getTable(target, columnOids, new OID("3"), new OID("10")); for (TableEvent e : l) { System.out.println(e); } } }