snmp4j完整示例
来源:互联网 发布:淘宝u盘32g多少钱 编辑:程序博客网 时间:2024/05/25 05:35
- #mib.properties
- #Fri Jul 11 15:57:28 CST 2008
- 1.3.6.1.2.1.1.6.0=beijing
- 1.3.6.1.2.1.1.8.0=test
- 1.3.6.1.2.1.1.5.0=admin
- 1.3.6.1.2.1.1.7.0=8899
mib搞的两天不是很明白,于是自己定义了个配置文件来充当mib库,mib.properties
下面的例子是服务器端,也就是manager:
- 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:192.168.10.191/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, 7,
- 0 }), new OctetString("8899")));
- 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, 6,
- 0 })));// pcName
- pdu.setType(PDU.GET);
- readResponse(sendPDU(pdu));
- }
- public void readResponse(ResponseEvent respEvnt) {
- // 解析Response
- System.out.println("----------解析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) {
- System.out.println("----------start-------------");
- try {
- SnmpUtil util = new SnmpUtil();
- util.initComm();
- util.setPDU();
- util.getPDU();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
agent代理端代码:
- import java.util.*;
- /**
- * agent代理端
- * leo
- * 20080710
- */
- import org.snmp4j.*;
- import org.snmp4j.smi.*;
- import snmputil.Config;
- public class OTAAgent {
- public static class Handler implements org.snmp4j.CommandResponder {
- protected java.lang.String mAddress = null;
- protected int mPort = 0;
- protected java.lang.String mMyCommunityName = null;
- protected org.snmp4j.TransportMapping mServerSocket = null;
- protected org.snmp4j.Snmp mSNMP = null;
- public Handler() {
- }
- public void configure() {
- mAddress = "192.168.10.191";
- mPort = 161;
- mMyCommunityName = "OAagent";
- }
- public void start() {
- try {
- mServerSocket = new org.snmp4j.transport.DefaultUdpTransportMapping(
- new org.snmp4j.smi.UdpAddress(java.net.InetAddress
- .getByName(mAddress), mPort));
- mSNMP = new org.snmp4j.Snmp(mServerSocket);
- mSNMP.addCommandResponder(this);
- mServerSocket.listen();
- } catch (java.net.UnknownHostException vException) {
- System.out.println(vException);
- } catch (java.io.IOException vException) {
- System.out.println(vException);
- }
- }
- public synchronized void processPdu(
- org.snmp4j.CommandResponderEvent aEvent) {
- java.lang.String vCommunityName = new java.lang.String(aEvent
- .getSecurityName());
- System.out.println("Community name " + vCommunityName);
- org.snmp4j.PDU vPDU = aEvent.getPDU();
- Config config=new Config();
- if (vPDU == null) {
- System.out.println("Null pdu");
- } else {
- System.out.println("(rcv) " + vPDU.toString());
- switch (vPDU.getType()) {
- case org.snmp4j.PDU.GET:
- case org.snmp4j.PDU.GETNEXT:
- break;
- case org.snmp4j.PDU.SET:
- System.out.println("------SET----------");
- String reciv=vPDU.get(0).getVariable().getSyntaxString();
- System.out.println("----set------"+vPDU.get(0).toString());
- String setoid=vPDU.get(0).toString();
- System.out.println("-----set-----"+setoid.substring(0, setoid.indexOf("=")-1));
- System.out.println("-----set-----"+setoid.substring(setoid.indexOf("=")+1));
- config.setValueByOID(setoid.substring(0, setoid.indexOf("=")-1).trim(), setoid.substring(setoid.indexOf("=")+1).trim());
- }
- org.snmp4j.mp.StatusInformation statusInformation = new org.snmp4j.mp.StatusInformation();
- org.snmp4j.mp.StateReference ref = aEvent.getStateReference();
- try {
- System.out.println("Sending response");
- vPDU.setType(PDU.RESPONSE);
- OID oid=vPDU.get(0).getOid();
- String setoid=vPDU.get(0).toString();
- System.out.println("----get------"+setoid.substring(0, setoid.indexOf("=")-1));
- System.out.println("-----get-----"+setoid.substring(setoid.indexOf("=")+1));
- vPDU.set(0, new VariableBinding(oid,
- new OctetString(config.getValueByOID(setoid.substring(0, setoid.indexOf("=")-1).trim()))));
- aEvent.getMessageDispatcher().returnResponsePdu(
- aEvent.getMessageProcessingModel(),
- aEvent.getSecurityModel(),
- aEvent.getSecurityName(),
- aEvent.getSecurityLevel(), vPDU,
- aEvent.getMaxSizeResponsePDU(), ref,
- statusInformation);
- } catch (org.snmp4j.MessageException vException) {
- System.out.println(vException);
- }
- }
- }
- }
- public static void main(String argv[]) {
- Handler h = new Handler();
- /** 初始化参数 * */
- h.configure();
- h.start();
- /** Do nothing loop * */
- while (true) {
- System.out.println("----------loop-------------");
- synchronized (OTAAgent.class) {
- try {
- OTAAgent.class.wait();
- } catch (Exception e) {
- }
- }
- }
- }
- }
还有一个就是获取和保存mib信息的类:
- package snmputil;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Hashtable;
- import java.util.Map;
- import java.util.Properties;
- public class Config {
- Properties properties;
- Map map;
- public Config() {
- properties = new Properties();
- try {
- properties.load(new FileInputStream("mib.properties"));
- } catch (IOException e) {
- System.out.println("读取properties文件错误");
- e.printStackTrace();
- }
- }
- /**
- * 根据oid获取value
- *
- * @param oid
- * @return
- */
- public String getValueByOID(String oid) {
- return properties.getProperty(oid);
- }
- public void setValueByOID(String oid, String value) {
- properties.setProperty(oid, value);
- try {
- properties.store(new FileOutputStream("mib.properties"),"mib.properties");
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //测试主函数
- public static void main(String[] args) {
- Config cfg=new Config();
- String oid="1.3.6.1.2.1.1.8.0";
- System.out.println("---------"+cfg.getValueByOID(oid));
- cfg.setValueByOID(oid, "test");
- System.out.println("---------"+cfg.getValueByOID(oid));
- }
- }
0 0
- snmp4j完整示例
- snmp4j完整示例
- snmp4j简单示例
- SNMP4j实现Trap的示例
- 利用SNMP4J实现Snmp Trap完整例子
- 利用SNMP4J实现Snmp Trap完整例子
- 利用SNMP4J实现Snmp Trap完整例子
- 利用SNMP4J实现Snmp Trap完整例子
- 利用SNMP4J实现 Snmp Trap 完整例子
- SNMP4J
- SNMP4J
- SNMP4J
- SNMP4j实现同步和异步的Walk的示例
- SNMP4j实现同步和异步的GET的示例
- AdvancedDataGrid完整示例
- AdvancedDataGrid完整示例
- Spring + iBATIS完整示例
- nginx完整配置示例
- input 复选框 取消和选中触发事件响应
- iOS开发——Autolayout的Top Layout guide问题
- 用RadASM开发窗口程序(2)
- Session管理
- ios开发者中心,证书,APPID,配置文件
- snmp4j完整示例
- Unity学习日常问题记录四 -- 改变进度条上的图标
- CentOS中ThinkPHP下的http500
- 错误的洗牌算法
- 取球问题
- NSDictionary的使用及常用方法(如实始化、添加元素、删除元素、修改元素值等)
- Redis的安装与使用
- SSL/TLS协议运行机制的概述
- ExtJs常用布局--layout详解(含实例)