SNMP4J SNMP用法小记

Posted 星仔学习

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);

              // AgentPDU,并接收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);

              // AgentPDU,并返回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 ), newOctetString("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 SNMP用法小记的主要内容,如果未能解决你的问题,请参考以下文章

SNMP4J与ObjectSNMP对比分析之我见

java-snmp4j之snmp

转载 java利用snmp4j包来读取snmp协议数据(Manager端)

如何配置引擎 ID 以使用 SNMP4J 发送 SNMP 陷阱 V3?

snmp4j 异步获取节点信息

如何解析 mibs 以获取 snmp4j 的陷阱消息