snmp簡介,報文,mib,oid瞭解網:https://www.cnblogs.com/chloneda/p/snmp-protocol.htmlhtml
發送udp請求須要的jar包 snmp4j官網連接 java
snmp簡單案列:https://blog.csdn.net/FormatWindowsXP/article/details/81703423 (我用的jar包是snmp4j-2.8.0.jar)ios
snmp4j.jar連接: https://pan.baidu.com/s/1EN2Od3qlFEf3y2edorQ8Sw 提取碼: x34k 複製這段內容後打開百度網盤手機App,操做更方便哦app
1. 監聽162端口tcp
import java.util.Vector; import org.snmp4j.CommandResponder; import org.snmp4j.CommandResponderEvent; import org.snmp4j.MessageDispatcherImpl; import org.snmp4j.Snmp; import org.snmp4j.TransportMapping; import org.snmp4j.mp.MPv1; import org.snmp4j.mp.MPv2c; import org.snmp4j.mp.MPv3; import org.snmp4j.security.SecurityModels; import org.snmp4j.security.SecurityProtocols; import org.snmp4j.security.USM; import org.snmp4j.smi.Address; import org.snmp4j.smi.GenericAddress; import org.snmp4j.smi.OctetString; import org.snmp4j.smi.TcpAddress; import org.snmp4j.smi.UdpAddress; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultTcpTransportMapping; import org.snmp4j.transport.DefaultUdpTransportMapping; import org.snmp4j.util.MultiThreadedMessageDispatcher; import org.snmp4j.util.ThreadPool; /** * @author Song * snmp協議的監聽模塊,用於監聽snmp客戶端發來的信息. * 使用snmp4j實現snmp消息監聽, * snmp4j 版本 2.6.2 * snmp trap 默認端口162 * 2018年08月14日 創建對象 **/ public class ReceiveService_snmp implements CommandResponder//使用snmp4j須要實現一個接口 { public static void main(String[] args) { System.out.println("v1="+MPv1.ID); System.out.println("v2="+MPv2c.ID); System.out.println("v3="+MPv3.ID); ReceiveService_snmp snmp_receive = new ReceiveService_snmp(); snmp_receive.run(); //開始監聽 } //#region 啓動程序 public void run() { try { startListen(); snmp.addCommandResponder(this); System.out.println("開始監聽Trap信息!"); } catch (Exception ex) { ex.printStackTrace(); } } //#endregion //#region 啓動監聽 private static Snmp snmp = null; private static Address listenAddress; private static void startListen(){ try{ ThreadPool threadPool= ThreadPool.create("Trap", 2);//啓動一個線程池,設置最高兩個線程 MultiThreadedMessageDispatcher dispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl()); listenAddress = (Address) GenericAddress.parse("udp:0.0.0.0/162"); //監聽全部ip的162端口 tcp:0.0.0.0/162 TransportMapping transport = null; // 對TCP與UDP協議進行處理 try { if (listenAddress instanceof UdpAddress) { System.out.println("使用UDP協議"); transport = new DefaultUdpTransportMapping((UdpAddress) listenAddress); } else { System.out.println("使用TCP協議"); transport = new DefaultTcpTransportMapping((TcpAddress) listenAddress); } }catch (Exception e){ System.out.println("接收地址綁定失敗,端口162可能被佔用,錯誤信息:"+e.getMessage()); System.exit(0);//綁定失敗,中止當前進程 } snmp = new Snmp(dispatcher, transport); snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1()); snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c()); snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3()); USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0); SecurityModels.getInstance().addSecurityModel(usm); snmp.listen(); //開始監聽 } catch (Exception e){ System.out.println("啓動監聽時觸發catch異常:"+e.getMessage()); } } //#endregion //#region processPdu接口具體的實現 /** * processPdu接口具體的實現 * 在接收到snmp信息時,會觸發這個方法 **/ public void processPdu(CommandResponderEvent respEvnt){ if (respEvnt != null && respEvnt.getPDU() != null){ Vector<VariableBinding> recVBs = (Vector<VariableBinding>) respEvnt.getPDU().getVariableBindings();//這裏提示我類型轉換錯誤,我必需要進行一次強轉 System.out.println("=======================消息內容======================="); System.out.println("數據來源:"+respEvnt.getPeerAddress()); System.out.println("團體名:"+new String(respEvnt.getSecurityName()));//團體名在v1 v2c下能夠用來作認證 String snmpVer = respEvnt.getMessageProcessingModel()==0? "v1":respEvnt.getMessageProcessingModel()==1? "v2c":respEvnt.getMessageProcessingModel()==3? "v3":"其餘版本";//其實這裏還有一個v2u版本,但網上不多說起不知道是否是應用較少? System.out.println("trap版本:"+snmpVer); System.out.println(respEvnt.toString());//輸出一下整條信息 for (int i = 0; i < recVBs.size(); i++) { VariableBinding recVB = recVBs.elementAt(i); System.out.println("oid:"+recVB.getOid()); System.out.println("variable:"+recVB.getVariable()); } System.out.println("=======================輸出結束======================="); } } //#endregion }
2. 發送UDP請求 案列:參考文章學習
import java.io.IOException; 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.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; public class Monitor { // systemDescr https://www.cnblogs.com/aspx-net/p/3554044.html static final String OID1 = "1.3.6.1.2.1.1.1.0"; // eth0 static final String OID2 = "1.3.6.1.2.1.2.2.1.2.2"; // MAC static final String OID3 = "1.3.6.1.2.1.2.2.1.6.2"; static final String OID4 = "1.3.6.1.2.1.1.1"; // //設備名稱 // static final String OID5 = "1.3.6.1.2.1.1.5.0"; //static final int [] OID5 = {1,3,6,1,2,1,1,2}; 與第一個同樣 /** * @param args */ public static void main(String[] args) { GetOID(); } /** * 得到本機oid */ public static void GetOID() { TransportMapping transport = null; try { // 傳輸協議udp transport = new DefaultUdpTransportMapping(); transport.listen(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // 構造報文 PDU request = new PDU(); request.add(new VariableBinding(new OID(OID1))); request.add(new VariableBinding(new OID(OID2))); request.add(new VariableBinding(new OID(OID3))); request.add(new VariableBinding(new OID(OID4))); // request.add(new VariableBinding(new OID(OID5))); request.setType(PDU.GET); // snmp初始化 Snmp snmp = new Snmp(transport); // 構造目標 CommunityTarget target = new CommunityTarget(); Address localAddress =GenericAddress.parse("udp:10.10.11.168/161"); target.setAddress(localAddress); OctetString oct = new OctetString("public"); target.setCommunity(oct); target.setVersion(SnmpConstants.version2c); target.setRetries(2); target.setTimeout(5 * 60); try { // 發送報文 ResponseEvent responseEvent = snmp.send(request, target); PDU response = responseEvent.getResponse(); if (response != null) { System.out.println("request.size()=" + request.size()); System.out.println("response.size()=" + response.size()); VariableBinding vb1 = response.get(0); VariableBinding vb2 = response.get(1); VariableBinding vb3 = response.get(2); VariableBinding vb4 = response.get(3); VariableBinding vb5 = response.get(4); System.out.println(vb1); System.out.println(vb2); System.out.println(vb3); System.out.println(vb4); // System.out.println(vb5); //釋放 transport.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
3. 安裝mg-softthis
百度雲盤下載rar壓縮包,裏面有個安裝教程文檔spa
連接: https://pan.baidu.com/s/1f_aVEy1aiwr_kPalyH-fOw 提取碼: dyva 複製這段內容後打開百度網盤手機App,操做更方便哦.net
(說明:MIB Tree->ios->org->dod->internet->mgmt ‘公有oid’,private '私有oid'線程
右鍵Walk爲矢量查詢,Get,GetNext 爲標量查詢.
選中Query results 返回結果節點右鍵 Properties ‘能夠查看OID,加上節點後的(.數字)就是這個結果的oid’
)
注意事項:1.以管理員身份運行: 解壓後的setup.exe(謹慎起見) ,MG-SOFT\MIB Browser\Bin\mgmibbpe.exe程序(MIB Browser)
4. 導入MIB學習網