- 浏览: 40715 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
aunox:
windows进程呢,怎么监控,知道不?
SNMP windows OIDs -
akun1982:
韩国世界杯,那届世界杯可以说结果让很多人多少有些失望,巴西走了 ...
中国金牌第一是不是一件好事
作者: ecsun 链接:http://papa.iteye.com/blog/220534 发表时间: 2008年07月28日
声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!
Java代码
#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.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:
Java代码
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();
}
}
}
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代理端代码:
Java代码
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) {
}
}
}
}
}
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信息的类:
Java代码
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));
}
}
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 人发表留言,猛击->>这里<<-参与讨论
JavaEye推荐
相关推荐
SNMP4J异步调用示例,实际项目中的源码
博文链接:https://avery-leo.iteye.com/blog/213980
jar/SNMP4J.jar jar/snmp4jclt-1.2.1.zip jar/snmp.jar 基于java的Oid获取软件.rar Java进行SNMP通信的指南SnmpProgrammingGuideViaJava.doc Java实现snmp的get和walk代码示例.doc snmp4j.chm snmp.chm SnmpMain....
博文链接:https://avery-leo.iteye.com/blog/210931
非常好的java写SNMP的入门资料。分享给大家!
以下是使用SNMP4j库实现SNMP的Get功能的示例代码: ```java public class SnmpGet { private static int version = SnmpConstants.version1; private static String protocol = "udp"; private static int port ...
snmp-tutorialSNMP tutorial :Jrobin、SNMP4jsnmp4j-1x-demoSNMP4j实现同步和异步的GET的示例SNMP4j实现同步和异步的Walk的示例SNMP4j实现Trap的示例SNMP4j实现SET的示例SNMP4j实现GETBLUK的示例robin-demoJRobin ...
简单的snmp-nms 使用Java和SNMP4J制作的SNMP NMS和代理的示例 用法 指示尚未完成。
一份基于snmp4j开发的snmp源代码工具包,SNMPSample类有详细的示例程序,需要使用snmp4j开发的朋友,值得研究一下。
snmp4j 一起使用的 Clojure 库。 它公开了 2 个 Java 类以在 Java 环境中工作:com.enterpriseweb.snmp.V2 和 com.enterpriseweb.snmp.V3 Java 测试示例 package test; import org.json.JSONObject; import org....
安卓SNMP 这是Android设备上的简单网络管理协议(SNMP)的示例实现。 我们设计了一个管理信息库(MIB),并实现了一个用于查询网络上设备的桌面管理器和一个用于响应查询的Android客户端。MIB如何使用在连接了与将...
Blaze API -(未来)用于 Blaze IO 和/或 SNMP4J 的高级 API。 由 Scott Abernethy (github @scott-abernethy) 创建。当前状态Blaze 在新的 Akka v2.2 IO 库之上实现了 SNMP 协议。 支持的操作: SNMP 获取。基本...
adapters.snmp4j:使用出色的SNMP4J框架构建的适配器。 adapters.tools:用于测试适配器实现的工具。 svc:陷阱处理器示例。 用法 将以下物品添加到您的leiningen :dependencies [eu.obliquo/herark "0.1.13-...
MibbleBrowser,jmibbrowser,运行zhtelecombasis示例程序,请将系统时间改为2012年6月份,这个代码是加密过的,偶尝试反编译破解了一下,有部分动态加载运行的代码没完成,懒得研究了,还不如去玩下snmp4j。