Chinaunix首页 | 论坛 | 博客
  • 博客访问: 18968
  • 博文数量: 16
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 100
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-04 10:47
文章分类
文章存档

2012年(16)

我的朋友
最近访客

分类:

2012-03-04 10:52:28

原文地址:snmp4j完整实例 作者:zhengdong1987

Java代码
  1. #mib.properties   
  2. #Fri Jul 11 15:57:28 CST 2008  
  3. 1.3.6.1.2.1.1.6.0=beijing   
  4. 1.3.6.1.2.1.1.8.0=test   
  5. 1.3.6.1.2.1.1.5.0=admin   
  6. 1.3.6.1.2.1.1.7.0=8899  

mib搞的两天不是很明白,于是自己定义了个配置文件来充当mib库,mib.properties

下面的例子是服务器端,也就是manager:

Java代码
  1. import java.io.IOException;   
  2.   
  3. import java.util.Vector;   
  4.   
  5. import org.snmp4j.CommunityTarget;   
  6.   
  7. import org.snmp4j.PDU;   
  8.   
  9. import org.snmp4j.Snmp;   
  10.   
  11. import org.snmp4j.TransportMapping;   
  12.   
  13. import org.snmp4j.event.ResponseEvent;   
  14.   
  15. import org.snmp4j.mp.SnmpConstants;   
  16.   
  17. import org.snmp4j.smi.Address;   
  18.   
  19. import org.snmp4j.smi.GenericAddress;   
  20.   
  21. import org.snmp4j.smi.OID;   
  22.   
  23. import org.snmp4j.smi.OctetString;   
  24.   
  25. import org.snmp4j.smi.VariableBinding;   
  26.   
  27. import org.snmp4j.transport.DefaultUdpTransportMapping;   
  28.   
  29. public class SnmpUtil {   
  30.     private Snmp snmp = null;   
  31.     private Address targetAddress = null;   
  32.   
  33.     public void initComm() throws IOException {   
  34.         // 设置Agent方的IP和端口   
  35.          targetAddress = GenericAddress.parse("udp:192.168.10.191/161");   
  36.          TransportMapping transport = new DefaultUdpTransportMapping();   
  37.          snmp = new Snmp(transport);   
  38.          transport.listen();   
  39.      }   
  40.   
  41.     public ResponseEvent sendPDU(PDU pdu) throws IOException {   
  42.         // 设置 target   
  43.          CommunityTarget target = new CommunityTarget();   
  44.          target.setCommunity(new OctetString("public"));   
  45.          target.setAddress(targetAddress);   
  46.         // 通信不成功时的重试次数   
  47.          target.setRetries(2);   
  48.         // 超时时间   
  49.          target.setTimeout(1500);   
  50.          target.setVersion(SnmpConstants.version1);   
  51.         // 向Agent发送PDU,并返回Response   
  52.         return snmp.send(pdu, target);   
  53.      }   
  54.   
  55.     public void setPDU() throws IOException {   
  56.         // set PDU   
  57.          PDU pdu = new PDU();   
  58.          pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 7,   
  59.                 0 }), new OctetString("8899")));   
  60.          pdu.setType(PDU.SET);   
  61.          sendPDU(pdu);   
  62.      }   
  63.     public void getPDU() throws IOException {   
  64.         // get PDU   
  65.          PDU pdu = new PDU();   
  66.          pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 6,   
  67.                 0 })));// pcName   
  68.          pdu.setType(PDU.GET);   
  69.          readResponse(sendPDU(pdu));   
  70.      }   
  71.   
  72.     public void readResponse(ResponseEvent respEvnt) {   
  73.         // 解析Response   
  74.          System.out.println("----------解析Response-------------");   
  75.         if (respEvnt != null && respEvnt.getResponse() != null) {   
  76.              Vector recVBs = respEvnt.getResponse()   
  77.              .getVariableBindings();   
  78.             for (int i = 0; i < recVBs.size(); i++) {   
  79.                  VariableBinding recVB = recVBs.elementAt(i);   
  80.                  System.out   
  81.                          .println(recVB.getOid() + " : " + recVB.getVariable());   
  82.              }   
  83.          }   
  84.   
  85.      }   
  86.   
  87.     public static void main(String[] args) {   
  88.          System.out.println("----------start-------------");   
  89.         try {   
  90.              SnmpUtil util = new SnmpUtil();   
  91.              util.initComm();   
  92.               util.setPDU();   
  93.              util.getPDU();   
  94.          } catch (IOException e) {   
  95.   
  96.              e.printStackTrace();   
  97.   
  98.          }   
  99.   
  100.      }   
  101.   
  102. }  
阅读(381) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~