Chinaunix首页 | 论坛 | 博客
  • 博客访问: 411396
  • 博文数量: 101
  • 博客积分: 2324
  • 博客等级: 大尉
  • 技术积分: 887
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-19 19:28
文章分类

全部博文(101)

文章存档

2012年(3)

2011年(60)

2010年(38)

分类: 系统运维

2010-11-23 15:36:46

snmp4j的jar包可以在它的官方网站上下载,我就不啰嗦了。
接下来直接贴代码:
view plaincopy to clipboardprint?
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 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 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添加写入权限了。
    接下来就可以编写代码了,我把上面的例子重构一下,代码如下:
 接下来就可以编写代码了,我把上面的例子重构一下,代码如下:
view plaincopy to clipboardprint?
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 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 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下测试成功。
 
 
阅读(1581) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

chinaunix网友2010-11-24 14:38:00

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com