上一篇文章讲了Snmp的一些基本概念,接下来,我们使用Java的开源组件snmp4j来实现一下Snmp里的各种功能。首先是上一篇文章中的那个例子。即通过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();
- }
- }
- }
- 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();
- }
- }
- }
- 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”的消息,就说明我们的操作成功啦!
以上代码在WindowsXP下测试成功。
前一篇文章讲了如何用snmp4j实现set和get的功能,今天讲如何接收trap。
snmp4j提供了一个抽象类CommandResponder 类用于接收trap,这个类里面有一个必须实现 的方法processPdu() ,当接收到trap,会自动进入这个方法,因此我们可以将对trap的处理写在这里。
- 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();
- }
- 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();
- }
- }
- 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." );
- }
- 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的收发成功了。