1. 程式人生 > 其它 >用snmp4j實現Snmp功能

用snmp4j實現Snmp功能

snmp4j的jar包可以在它的官方網站http://www.snmp4j.org/上下載

接下來直接貼程式碼:

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;

public class SnmpUtil {

private Snmp snmp = null;

private Address targetAddress = null

;

public void 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 void 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(new int[] { 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());

}

}

}

public static void 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;

public class SnmpUtil {

private Snmp snmp = null;

private Address targetAddress = null;

public void 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

return snmp.send(pdu, target);

}

public void setPDU() throws IOException {

// set PDU

PDU pdu = new PDU();

pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 }), new OctetString("SNMPTEST")));

pdu.setType(PDU.SET);

sendPDU(pdu);

}

public void getPDU() throws IOException {

// get PDU

PDU pdu = new PDU();

pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 })));

pdu.setType(PDU.GET);

readResponse(sendPDU(pdu));

}

public void 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());

}

}

}

public static void 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”的訊息,就說明我們的操作成功啦!

前一篇文章講了如何用snmp4j實現set和get的功能,今天講如何接收trap。

snmp4j提供了一個抽象類CommandResponder類用於接收trap,這個類裡面有一個必須實現的方法processPdu(),當接收到trap,會自動進入這個方法,因此我們可以將對trap的處理寫在這裡。

下面修改上篇文章例子中的initComm()方法:

private TransportMapping transport = null;

public void initComm() throws IOException {

// 設定Agent方的IP和埠

targetAddress = GenericAddress.parse("udp:192.168.1.1/161");

// 設定接收trap的IP和埠

transport = new DefaultUdpTransportMapping(new UdpAddress(

"192.168.1.2/162"));

snmp = new Snmp(transport);

CommandResponder trapRec = new CommandResponder() {

public synchronized void processPdu(CommandResponderEvent e) {

// 接收trap

PDU command = e.getPDU();

if (command != null) {

System.out.println(command.toString());

}

}

};

snmp.addCommandResponder(trapRec);

transport.listen();

}

其中targetAddress指Agent端也就是trap傳送,transport指trap接收方,這裡就是本機,假設IP是192.168.1.2,但注意不能寫成127.0.0.1。

因為我們無法得知trap什麼時候會發送,所以需要有一個執行緒等待trap的到來,在這個例子中我們使用wait()來等待trap的到來,具體應用中就要根據實際情況來做了。

public synchronized void listen() {

System.out.println("Waiting for traps..");

try {

this.wait();//Wait for traps to come in

} catch (InterruptedException ex) {

System.out.println("Interrupted while waiting for traps: " + ex);

System.exit(-1);

}

}

public static void main(String[] args) {

try {

SnmpUtil util = new SnmpUtil();

util.initComm();

util.listen();

} catch (IOException e) {

e.printStackTrace();

}

}

將上面的程式碼新增到原來的例子中,就可以接收trap了。

但是還有一個問題,如何讓192.168.1.1傳送trap呢?這個也可以使用snmp4j來做。其實發送trap和傳送set、get PDU是類似的,同樣是傳送PDU,只不過型別不一樣。我們把前面的例子複製到192.168.1.1,在裡面新增一段程式碼:

public void setTrap() throws IOException {

// 構造Trap PDU

PDU pdu = new PDU();

pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.1"),

new OctetString("SnmpTrap")));

pdu.setType(PDU.TRAP);

sendPDU(pdu);

System.out.println("Trap sent successfully.");

}

這裡PDU的OID和Value可以自己構造,無需使用特定的值。

然後修改地址
targetAddress = GenericAddress.parse("udp:192.168.1.2/162");
transport = new DefaultUdpTransportMapping();

另外需要修改target的version,即改為target.setVersion(SnmpConstants.version2c)為什麼要這樣改我也沒搞清楚,總之verion1收不到。

接下來修改main()函式,呼叫setTrap()。

然後回到本機執行剛才的例子,當控制檯顯示“Waiting for traps..”時,執行Agent端的例子。此時如果192.168.1.2打出我們剛剛設定的PDU的資訊,就說明Trap的收發成功了。