使用snmp4j實現Snmp功能(一)
插播一段宣告:樓主我其實就用了小半年的Snmp而已,就把學習心得發了出來,目的就是能夠引導大家入門,但是更深入的知識我就不知道啦。請大家不要再問我問題了,我知道的都已經寫出來啦,謝謝!
上一篇有關Snmp的文章已經是一年前寫的了,因為工作等各種原因,一直沒有繼續下去。但是不管怎麼樣,包括AppFuse,雖然速度有點慢,我還是會堅持學習並將心得寫下去。
上一篇文章講了Snmp的一些基本概念(Snmp學習筆記),接下來,我們使用Java的開源元件snmp4j來實現一下Snmp裡的各種功能。首先是上一篇文章中的那個例子。即通過snmp獲取機器名。
接下來直接貼程式碼:
import java.io.IOException; import java.util.Vector; 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; publicclass SnmpUtil { private Snmp snmp = null; private Address targetAddress = null; publicvoid initComm() throws IOException { // 設置Agent方的IP和埠 targetAddress = GenericAddress.parse TransportMapping transport = new DefaultUdpTransportMapping(); snmp = new Snmp(transport); transport.listen(); } publicvoid sendPDU() throws IOException { // 設置 target CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString("public")); target.setAddress(targetAddress); // 通訊不成功時的重試次數 target.setRetries(2); // 超時時間 target.setTimeout(1500); target.setVersion(SnmpConstants.version1); // 創建 PDU PDU pdu = new PDU(); pdu.add(new VariableBinding(new OID(newint[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 }))); // MIB的訪問方式 pdu.setType(PDU.GET); // 向Agent發送PDU,並接收Response ResponseEvent respEvnt = snmp.send(pdu, target); // 解析Response if (respEvnt != null && respEvnt.getResponse() != null) { Vector<VariableBinding> recVBs = respEvnt.getResponse() .getVariableBindings(); for (int i = 0; i < recVBs.size(); i++) { VariableBinding recVB = recVBs.elementAt(i); System.out.println(recVB.getOid() + " : " + recVB.getVariable()); } } } publicstaticvoid main(String[] args) { try { SnmpUtil util = new SnmpUtil(); util.initComm(); util.sendPDU(); } catch (IOException e) { e.printStackTrace(); } } } |
上面的這段程式碼直接參考snmp4j API說明文件中提供的例子,是一個最簡單的snmp4j的應用。只要你的機器裡安裝了snmp通訊元件,上面的程式碼應該可以執行成功。
在上一個例子中,我們只做了讀取的工作,接下來,我們進行一下設定操作,通過Snmp修改讀取的機器名。
public的預設許可權是隻讀,要想進行寫操作,我們必須進行手動的設定。具體的做法是:進入管理工具→服務,找到Snmp Service→屬性→安全。在這個選項卡中我們可以看到public的許可權是隻讀,你可以修改public的許可權,也可以重新建立一個community。從安全形度來講當然應該新建一個,在這裡為了測試方便,我就直接給public新增寫入許可權了。
接下來就可以編寫程式碼了,我把上面的例子重構一下,程式碼如下:
import java.io.IOException; import java.util.Vector; 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; publicclass SnmpUtil { private Snmp snmp = null; private Address targetAddress = null; publicvoid initComm() throws IOException { // 設置Agent方的IP和埠 targetAddress = GenericAddress.parse("udp:127.0.0.1/161"); TransportMapping transport = new DefaultUdpTransportMapping(); snmp = new Snmp(transport); transport.listen(); } public ResponseEvent sendPDU(PDU pdu) throws IOException { // 設置 target CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString("public")); target.setAddress(targetAddress); // 通訊不成功時的重試次數 target.setRetries(2); // 超時時間 target.setTimeout(1500); target.setVersion(SnmpConstants.version1); // 向Agent發送PDU,並返回Response returnsnmp.send(pdu, target); } publicvoid setPDU() throws IOException { // set PDU PDU pdu = new PDU(); pdu.add(new VariableBinding(new OID(newint[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 }), new OctetString("SNMPTEST"))); pdu.setType(PDU.SET); sendPDU(pdu); } publicvoid getPDU() throws IOException { // get PDU PDU pdu = new PDU(); pdu.add(new VariableBinding(new OID(newint[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 }))); pdu.setType(PDU.GET); readResponse(sendPDU(pdu)); } publicvoid readResponse(ResponseEvent respEvnt) { // 解析Response if (respEvnt != null && respEvnt.getResponse() != null) { Vector<VariableBinding> recVBs = respEvnt.getResponse() .getVariableBindings(); for (int i = 0; i < recVBs.size(); i++) { VariableBinding recVB = recVBs.elementAt(i); System.out.println(recVB.getOid() + " : " + recVB.getVariable()); } } } publicstaticvoid main(String[] args) { try { SnmpUtil util = new SnmpUtil(); util.initComm(); util.setPDU(); util.getPDU(); } catch (IOException e) { e.printStackTrace(); } } } |
如果控制檯打出“1.3.6.1.2.1.1.5.0 : SNMPTEST”的訊息,就說明我們的操作成功啦!
以上程式碼在WindowsXP下測試成功。