问个snmp的问题。

xlbc 2014-12-18 03:52:07
我是连接我自己的电脑,win7 32的。
程序是直接用的这个:
http://cuisuqiang.iteye.com/blog/1539434

package t1;

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;

/**
* @说明 SNMP4J测试
* @author cuisuqiang
* @version 1.0
* @since
*/
public class SnmpUtil {
private Snmp snmp = null;
private Address targetAddress = null;

public void initComm() throws IOException {
// 设置Agent方的IP和端口
targetAddress = GenericAddress.parse("udp:192.168.0.148/22500");
TransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
transport.listen();
}

public ResponseEvent sendPDU(PDU pdu) throws IOException {
// 设置 目标
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
// 通信不成功时的重试次数 N+1次
target.setRetries(2);
// 超时时间
target.setTimeout(2 * 1000);
// SNMP 版本
target.setVersion(SnmpConstants.version2c);
// 向Agent发送PDU,并返回Response
return snmp.send(pdu, target);
}

public void getPDU() throws IOException {
// PDU 对象
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID("1.2.3.4.5.6")));
// 操作类型
pdu.setType(PDU.GET);
ResponseEvent revent = sendPDU(pdu);
if(null != revent){
readResponse(revent);
}
}

@SuppressWarnings("unchecked")
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().toString());
}
}
}

public static void main(String[] args) {
try {
SnmpUtil util = new SnmpUtil();
util.initComm();
util.getPDU();
} catch (IOException e) {
e.printStackTrace();

}
}
}


应该跑出来的是这个:
------------>解析Response<-------------
0.0 : xxx
1.3.6.1.2.1.1.6.0 : 12345
1.3.6.1.2.1.1.2.0 : 54321

但我的跑出来是这个:
------------>解析Response<-------------
1.2.3.4.5.6 : noSuchObject


请问是哪里不对吗?感觉是没写进去?
或者这个程序用的不是v3版,我本机电脑是v3?
...全文
176 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
冥王之锤 2014-12-19
  • 打赏
  • 举报
回复
udp:192.168.0.148/22500 对端端口是否是22500,协议规定是161端口。 // SNMP 版本 target.setVersion(SnmpConstants.version2c); 这段用的是v2c版本,对端如果v3是不可以的。
onlinetomcat 2014-12-19
  • 打赏
  • 举报
回复
如果你的是V3版本 还需要些事一些加密方式等属性
onlinetomcat 2014-12-19
  • 打赏
  • 举报
回复
你的oid是不对吧
xlbc 2014-12-19
  • 打赏
  • 举报
回复
解决了。。结贴,谢谢各位。

50,527

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧