SNMP4J是一个开源的,用Java实现的snmp协议。其中提供了一下API,在这些API上面封装了一些方法,比如SNMP的get-request请求,get-next-request请求等

如果不了解SNMP协议的点我(不是我写的)

下面贴我封装的Java代码(后面还会封装一些其他请求,比如说请求一个表格等操作)

SNMP4J jar包或源码下载点我(不用翻墙) www.snmp4j.org

定义接口Snmp_aip_interface.java

import java.util.List;

public interface Snmp_aip_interface {
/**
* 根据ip,oid返回get消息
* @param ip
* @param oid
* @return
*/
public String get(String ip, String oid_str); /**
* 根据ip,oid和新值设置oid节点的值
* @param ip
* @param oid_str
* @param value_new
*/
public void set(String ip, String oid_str, String value_new); /**
* 根据ip,oid返回getNext消息
* @param ip
* @param oid_str
* @return
*/
public String getNext(String ip, String oid_str); /**
* 根据ip,oid获取bulk消息
* @param ip
* @param oid_str
* @return
*/
public List<String> getBulk(String ip, String oid_str); /**
* snmp v3版get api
* @param ip
* @param oid_str
* @return
*/
public String getV3(String ip, String oid_str); /**
* 向ip地址发送消息,这里利用发送trap消息来发送任意消息
* @param ip
* @param message
*/
public void sendMessage(String ip, String message);
}

对接口的实现Snmp_api.java

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector; import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.UserTarget;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.PrivDES;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping; import com.gxf.snmpinterceptor.Snmp_aip_interface; /**
* 封装snmp v 2的消息格式,对外提供接口
* 使用开源snmp4j进行封装
* 代理端监听udp 161端口,管理端监听的是udp 162端口
* snmp2c提供的消息格式,Get,GetNext,Set,Reponse,Trap,GetBulk,
* @author Administrator
*
* PDU类型 版本(0)|共同体|PDU类型(0-3)|请求标识|差错索引|差错状态(0-5)|差错索引|名称|值|名称|值...
*
* trap
*
* |PDU类型(0-3)|企业|代理地址|trap类型(0-6)|特定代码|时间戳|名称|值..
*
*/
public class Snmp_api implements Snmp_aip_interface{
private Snmp snmp;
private TransportMapping transportMapping;
private int timeOut;
private int timesRetry; public Snmp_api(){
// Thread thread_rec = new receiveTrapThread();
// thread_rec.start();
// listen();
} private void init(){
try {
transportMapping = new DefaultUdpTransportMapping();
} catch (IOException e) {
System.out.println("init transportMapping failed!");
System.out.println(e.getMessage());
}
snmp = new Snmp(transportMapping);
timeOut = 1500;
timesRetry = 2;
} /**
* 根据ip,oid返回get消息
* @param ip
* @param oid
* @return
*/
public String get(String ip, String oid_str){
init();
String ret = "";
//构造PDU
PDU pdu = new PDU();
OID oid = new OID(oid_str);
VariableBinding vb = new VariableBinding(oid);
pdu.add(vb);
pdu.setType(PDU.GET); //构造target
String address_str = "udp:" + ip + "/161";
// String address_str = "udp:210.38.235.184/161";
Address address = GenericAddress.parse(address_str);
OctetString name_com = new OctetString("public");
CommunityTarget target = new CommunityTarget();
target.setAddress(address); //设置address
target.setRetries(timesRetry);
target.setTimeout(timeOut);
target.setVersion(SnmpConstants.version2c); //设置版本号
target.setCommunity(name_com); //设置团体名 try {
this.transportMapping.listen();
} catch (IOException e) {
System.out.println("transportMapping.listen() failed!");
System.out.println(e.getMessage());
}
ResponseEvent responseEvent = null;
//发送消息
try {
responseEvent = snmp.send(pdu, target);
} catch (IOException e) {
System.out.println("snmp.send(pdu, target) failed");
System.out.println(e.getMessage());
}
//获取结果
if(responseEvent != null){
PDU pdu_rep = responseEvent.getResponse();
for(int i = 0; i < pdu_rep.size(); i++){
VariableBinding vb_temp = pdu_rep.get(i);
Variable var_temp = vb_temp.getVariable();
ret += var_temp.toString();
}//for
}//if
try {
transportMapping.close(); //关闭正在进行的监听
} catch (IOException e) {
System.out.println("transportMapping.close() failed!");
System.out.println(e.getMessage());
}
return ret;
} /**
* 根据ip,oid和新值设置oid节点的值
* @param ip
* @param oid_str
* @param value_new
*/
public void set(String ip, String oid_str, String value_new){
init();
//构造PDU
PDU pdu = new PDU();
OID oid = new OID(oid_str);
VariableBinding vb = null; vb = new VariableBinding(oid, new OctetString(value_new)); pdu.add(vb);
pdu.setType(PDU.SET); //构造target
String address_str = "udp:" + ip + "/161";
CommunityTarget target = new CommunityTarget();
OctetString com_oct = new OctetString("public");
target.setCommunity(com_oct); //设置团体名
Address address = GenericAddress.parse(address_str);
target.setAddress(address); //设置address
target.setVersion(SnmpConstants.version2c); //设置版本号
target.setTimeout(1500);
target.setRetries(timesRetry); //发送pdu
try {
transportMapping.listen();
} catch (IOException e) {
System.out.println("listen failed in set pdu");
System.out.println(e.getMessage());
}
try {
snmp.send(pdu, target);
} catch (IOException e) {
System.out.println("send failed in send set message!");
System.out.println(e.getMessage());
} finally{
try {
transportMapping.close(); //关闭正在进行的监听
} catch (IOException e) {
System.out.println("transportMapping.close() failed!");
System.out.println(e.getMessage());
}
}
} /**
* 根据ip,oid返回getNext消息
* @param ip
* @param oid_str
* @return
*/
public String getNext(String ip, String oid_str){
init();
String ret = "";
//构造PDU
PDU pdu = new PDU();
OID oid = new OID(oid_str);
VariableBinding vb = new VariableBinding(oid);
pdu.add(vb);
pdu.setType(PDU.GETNEXT); //构造target
String address_str = "udp:" + ip + "/161"; //UDP和ip
CommunityTarget target = new CommunityTarget();
Address address = GenericAddress.parse(address_str);
target.setCommunity(new OctetString("public"));
target.setAddress(address);
target.setTimeout(timeOut);
target.setRetries(timesRetry);
target.setVersion(SnmpConstants.version2c); //设置版本号 ResponseEvent responseEvent = null; try {
this.transportMapping.listen(); //开始监听
} catch (IOException e1) {
System.out.println("transportMapping.listen() failed!");
System.out.println(e1.getMessage());
} //发送消息
try {
responseEvent = snmp.send(pdu, target);
if(responseEvent != null){ //组装返回的消息
PDU pdu_reponse = responseEvent.getResponse();
for(int i = 0; pdu_reponse != null && i < pdu_reponse.size(); i++){
VariableBinding vb_temp = pdu_reponse.get(i);
Variable var_temp = vb_temp.getVariable();
ret += var_temp.toString();
}
}
} catch (IOException e) {
System.out.println("snmp.send(pdu, target) failed! in getNext");
e.printStackTrace();
} finally{ //关闭监听
try {
this.transportMapping.close();
snmp.close();
} catch (IOException e) {
System.out.println("transportMapping.close() failed!");
System.out.println(e.getMessage());
}
}//finally return ret;
} /**
* 根据ip,oid获取bulk消息
* @param ip
* @param oid_str
* @return
*/
public List<String> getBulk(String ip, String oid_str){
init();
List<String> ret = new ArrayList<String>();
//构造PDU
PDU pdu = new PDU();
pdu.setType(PDU.GETBULK);
pdu.setMaxRepetitions(200);
pdu.setNonRepeaters(0);
OID oid = new OID(oid_str);
VariableBinding vb = new VariableBinding(oid);
pdu.add(vb); //构造target
String address_str = "udp:" + ip + "/161"; //UDP和ip
CommunityTarget target = new CommunityTarget();
Address address = GenericAddress.parse(address_str);
target.setCommunity(new OctetString("public"));
target.setVersion(SnmpConstants.version2c);
target.setAddress(address);
target.setTimeout(timeOut);
target.setRetries(timesRetry); try { //开始监听UDP端口
this.transportMapping.listen();
} catch (IOException e) {
System.out.println("transportMapping.listen() failed!");
System.out.println(e.getMessage());
} //发送消息
ResponseEvent responseEvent = null;
try {
responseEvent = this.snmp.send(pdu, target);
} catch (IOException e) {
System.out.println(".snmp.send(pdu, target) failed! in getBulk");
e.printStackTrace();
} if(responseEvent != null){
PDU pdu_rec = responseEvent.getResponse(); System.out.println(pdu_rec.size()); //输出为0,说明前面没有获取到数据
for(int i = 0; i < pdu_rec.size(); i++){
String str_temp = "";
VariableBinding vb_temp = pdu_rec.get(i);
str_temp += vb_temp.getOid() + " ";
str_temp += vb_temp.getVariable().toString();
ret.add(str_temp);
}//for
}//if try {
this.transportMapping.close();
} catch (IOException e) {
System.out.println("transportMapping.close() failed!");
System.out.println(e.getMessage());
} return ret;
} /**
* 接收trap消息线程
* @author Administrator
*
*/
private class receiveTrapThread extends Thread{
public void run(){
System.out.println("start run thread.."); try {
transportMapping.listen();
} catch (IOException e) {
System.out.println("listen failed in receiveTrapThread.run()!");
e.printStackTrace();
}
CommandResponder commandResponder = new CommandResponder(){
@Override
public void processPdu(CommandResponderEvent event) {
System.out.println("enter processPdu()...");
PDU pdu_receive = event.getPDU();
StringBuffer sb = new StringBuffer(); for(int i = 0; i < pdu_receive.size(); i++){
VariableBinding vb_temp = pdu_receive.get(i);
sb.append(vb_temp.getVariable().toString());
}//for
System.out.println(sb.toString());
} };
snmp.addCommandResponder(commandResponder);
}
} public synchronized void listen(){
System.out.println("start listen..");
try {
this.wait();
} catch (InterruptedException e) { e.printStackTrace();
}
} /* (non-Javadoc)
* @see com.gxf.snmp.interfaces.Snmp_aip_interface#getV3(java.lang.String, java.lang.String)
*/
@Override
public String getV3(String ip, String oid_str) {
Snmp snmp = null;
try {
snmp = new Snmp(new DefaultUdpTransportMapping()); USM usm = new USM(SecurityProtocols.getInstance(),
new OctetString(MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm); snmp.listen();
} catch (IOException e) {
e.printStackTrace();
}
UsmUser user = new UsmUser(
new OctetString("luckygxf"),
AuthMD5.ID, new OctetString("luckygxf"),
PrivDES.ID, new OctetString("luckygxf")
);
OctetString contextEngineId = new OctetString("0002651100[02]");
snmp.getUSM().addUser(new OctetString("nmsAdmin"), new OctetString("0002651100"), user);
snmp.getUSM().addUser(new OctetString("luckygxf"), user);
String address_str = ip + "/161";
// System.out.println("address_str = " + address_str);
Address address = new UdpAddress(address_str);
UserTarget target = new UserTarget();
target.setVersion(SnmpConstants.version3);
target.setAddress(address);
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setSecurityName(new OctetString("luckygxf"));
target.setTimeout(2000);
target.setRetries(1); ScopedPDU pdu = new ScopedPDU();
pdu.setType(PDU.GET);
pdu.setContextEngineID(contextEngineId);
VariableBinding vb = new VariableBinding(new OID(oid_str));
pdu.add(vb); ResponseEvent responseEvent = null;
try {
responseEvent = snmp.send(pdu, target);
} catch (IOException e) { e.printStackTrace();
}
StringBuffer sb = new StringBuffer(); //返回的字符串
if(null != responseEvent){
PDU pdu_rec = responseEvent.getResponse();
// System.out.println("pdu_rec.size() = " + pdu_rec.size());
if(pdu_rec != null){
Vector<? extends VariableBinding> vbs = pdu_rec.getVariableBindings();
for(VariableBinding vb_temp : vbs){
sb.append(vb_temp.getVariable().toString());
}//for
}
}//if try {
snmp.close();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString(); } /* (non-Javadoc)
* @see com.gxf.snmp.interfaces.Snmp_aip_interface#sendMessage(java.lang.String, java.lang.String)
*/
@Override
public void sendMessage(String ip, String message) {
//构造trap PDU
PDU pdu = new PDU();
VariableBinding vb = new VariableBinding(new OID("1.3.6.1.2.3377.10.1.1.1.1"), new OctetString(message));
pdu.add(vb);
pdu.setType(PDU.TRAP); //构造target
String address_str = "udp:" + ip + "/16200"; //这里发送端口为16200
CommunityTarget target = new CommunityTarget();
Address address = GenericAddress.parse(address_str);
target.setAddress(address);
target.setVersion(SnmpConstants.version2c);
target.setCommunity(new OctetString("public"));
target.setTimeout(2000);
target.setRetries(2);
TransportMapping<UdpAddress> tranportMapping = null;
try {
tranportMapping = new DefaultUdpTransportMapping();
tranportMapping.listen();
} catch (IOException e) {
System.out.println("sendTrap(String ip) failed in Snmp_api.sendTrap(String ip)!");
e.printStackTrace();
}
Snmp snmp = new Snmp(tranportMapping);
try {
ResponseEvent responseEvent = snmp.send(pdu, target); //得到回复的pdu
if(null != responseEvent){
StringBuffer sb = new StringBuffer();
PDU pdu_rec = responseEvent.getResponse();
for(int i = 0;pdu_rec != null && i < pdu_rec.size(); i++){
VariableBinding vb_temp = pdu_rec.get(i);
sb.append(vb.getVariable().toInt());
}//for System.out.println(sb.toString());
}
} catch (IOException e) {
System.out.println("snmp.send(pdu, target) failed in Snmp_api.sendTrap(String ip)!");
} try {
tranportMapping.close();
snmp.close(); //关闭tansportmapping and snmp 释放资源
} catch (IOException e) {
System.out.println("tranportMapping.close() and snmp.close() failed in Snmp_api.sendTrap(String ip)!");
e.printStackTrace();
} }
}

P.S.从google code中copy了一份到github中:https://github.com/luckygxf/flower

这个封装的功能更强大

对SNMP4J的一些封装的更多相关文章

  1. [C#] 简单的 Helper 封装 -- RegularExpressionHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. iOS开发之App间账号共享与SDK封装

    上篇博客<iOS逆向工程之KeyChain与Snoop-it>中已经提到了,App间的数据共享可以使用KeyChian来实现.本篇博客就实战一下呢.开门见山,本篇博客会封装一个登录用的SD ...

  3. Ajax实现原理,代码封装

    都知道实现页面的异步操作需要使用Ajax,那么Ajax到是怎么实现异步操作的呢? 首先需要认识一个对象 --> XMLHttpRequest 对象 --> Ajax的核心.它有许多的属性和 ...

  4. 用C语言封装OC对象(耐心阅读,非常重要)

    用C语言封装OC对象(耐心阅读,非常重要) 本文的主要内容来自这里 前言 做iOS开发的朋友,对OC肯定非常了解,那么大家有没有想过OC中NSInteger,NSObject,NSString这些对象 ...

  5. 【知识必备】RxJava+Retrofit二次封装最佳结合体验,打造懒人封装框架~

    一.写在前面 相信各位看官对retrofit和rxjava已经耳熟能详了,最近一直在学习retrofit+rxjava的各种封装姿势,也结合自己的理解,一步一步的做起来. 骚年,如果你还没有掌握ret ...

  6. 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)

    前言 首先声明一下,我这个是对WebUploader开源上传控件的二次封装,底层还是WebUploader实现的,只是为了更简洁的使用他而已. 下面先介绍一下WebUploader 简介: WebUp ...

  7. 封装集合(Encapsulate Collection)

    封装就是将相关的方法或者属性抽象成为一个对象. 封装的意义: 对外隐藏内部实现,接口不变,内部实现自由修改. 只返回需要的数据和方法. 提供一种方式防止数据被修改. 更好的代码复用. 当一个类的属性类 ...

  8. CSharpGL(29)初步封装Texture和Framebuffer

    +BIT祝威+悄悄在此留下版了个权的信息说: CSharpGL(29)初步封装Texture和Framebuffer +BIT祝威+悄悄在此留下版了个权的信息说: Texture和Framebuffe ...

  9. CSharpGL(7)对VAO和VBO的封装

    CSharpGL(7)对VAO和VBO的封装 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含10多个独立的Demo,更适合入门参考 ...

随机推荐

  1. 简单的 socket 代码

    TCP 编程 客户端代码 将键盘输入的字符发送到服务端,并将从服务端接收到的字符输出到终端 #!/usr/python3 import socket def socket_client(): s = ...

  2. 【guava】对象处理

    一,equals方法 我们在开发中经常会需要比较两个对象是否相等,这时候我们需要考虑比较的两个对象是否为null,然后再调用equals方法来比较是否相等,google guava库的com.goog ...

  3. P4855 MloVtry的idea

    $ \color{#0066ff}{ 题目描述 }$ MloVtry是一个脑洞很大的人,它总会想出一些奇奇怪怪的idea. 可问题是,MloVtry作为一个蒟蒻,很多时候都没办法解决自己提出的问题,所 ...

  4. Difference **面向过程(或者叫结构化)分析方法**面向对象分析方法

    面向过程和面向对象的区别 面向过程是分析出解决问题所需要的步骤,然后一步步实现,面向对象是把构成问事件分解成各个对象,建立对象的目的是为了描述某个事物在整个解决问题的步骤中的行为. 可以说面向对象是从 ...

  5. js 冒泡事件与解决冒泡事件

    事件冒泡 :当一个元素接收到事件的时候 会把他接收到的事件传给自己的父级,一直到window . html代码: <div id="div1"> <div id= ...

  6. TextInput

    TextInput /** TextInput 是一个允许用户在应用中通过键盘输入文本的基本组件* 本组件的属性提供了多种特性的配置,如自动完成,自动大小写,占位文字,键盘类型等* 常用:* plac ...

  7. Maven学习笔记(二)—— 整合SSH框架

    一.知识点准备 1.1 什么是依赖传递? 我们只添加一个struts2-core的依赖,结果会发现所有关于struts2的依赖都进来了. 因为我们的项目依赖struts2-core-2.3.24.ja ...

  8. SQL数据库“单个用户”不能访问,设置为多个用户的解决方法

    USE master; GO DECLARE @SQL VARCHAR(MAX); SET @SQL='' SELECT @SQL=@SQL+'; KILL '+RTRIM(SPID) FROM ma ...

  9. 未在本地计算机上注册microsoft.jet

      未在本地计算机上注册microsoft.jet http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=13255

  10. python-使用字典使Fibonacci更有效率

    原代码: def fib(n): if n == 1: return 1 elif n == 2: return 2 else: return fib(n-1)+fib(n-2) 改进后: def f ...