我正在尝试编写我的第一个snmp4j客户端。我有一个代理在192.168.60.105上运行。使用net-snmp,我可以查询OID并得到结果。使用smnp4j时,snmp get的响应事件将返回null响应和null错误。我认为消息超时了,但我不知道为什么。
我使用net-snmp得到一个结果
jgaer@ljgaer2_~: snmpget 192.168.60.105 .1.3.6.1.4.1.27675.20.5.2.0
CW-NET-STG-SVR-MIB::cwNetStgSvrProvisioningEnable.0 = Hex-STRING: 00 00 00 00
我尝试过使用更长的超时时间和更多的重试次数,但返回的时间更长。这就是为什么我认为我超时了。我就是不明白为什么。我也预料到,如果在超时时返回responseEvent,错误将表明这一点。我试过使用verions1。使用版本3需要范围内的PDU。
public static void main(String[] args) throws IOException {
String address = ("udp:192.168.60.105/161");
TransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
transport.listen();
PDU pdu = new PDU();
pdu.setType(PDU.GET);
pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.27675.20.5.2.0")));
Address targetAddress = GenericAddress.parse(address);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion(SnmpConstants.version2c);
ResponseEvent response = snmp.send(pdu,target);
System.out.println(response);
System.out.println(response.getResponse());
System.out.println(response.getError());
}
运行上述代码的结果
org.snmp4j.event.ResponseEvent[source=org.snmp4j.Snmp@3f91beef]
null
null
我希望错误或响应都不为空。我使用的是java版本java版本"1.8.0_191“和snmp4j版本2.5.0。代理正在运行2.5.3
我使用wireshark跟踪数据包,并可以确认我从未得到使用snmp4j的代理的响应。我对协议的了解不足以进行逐字节比较,但net-snmp调用的info列看起来与snmp4j调用非常不同。
net-snmp
length info
106 get-request : sent from client to agent
159 report 1.3.6.1.6.3.15.1.1.4.0 : sent from agent to client
192 encryptedPDI : privKey unknown : sent from client to agent
196 encryptedPDI : privKey unknown : sent from agent to client
snmp4j - response从未收到从客户端到代理的三条消息
89 get-request 1.3.6.1.4.1.27675.20.5.2.0 sent from client to agent
查看字节的文本编码版本,我看到字符串'public‘
发布于 2019-03-27 23:34:06
问题是代理是SNMPv3,它要求我使用一个带有一些授权信息的ScopedPDU。用户和密码短语是从~/.snmp/snmp.conf文件中获取的。我现在正在与代理连接,并从代理获得响应。代码如下所示。我没有得到正确的值,而是我发出了多少个get的计数器。但这是另一个问题。
第二个问题是为authProtocol和privProtocol使用了错误的值。学习的课程除了检查errorResponse之外,还检查响应的pdu类型。report的响应类型表示失败,报告的OID是失败原因的关键。
public static void main(String[] args) throws Exception {
TransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
OctetString localEngineId = new OctetString(MPv3.createLocalEngineID());
USM usm = new USM(SecurityProtocols.getInstance(), localEngineId, 0);
SecurityModels.getInstance().addSecurityModel(usm);
OctetString securityName = new OctetString("masked");
OID authProtocol = AuthMD5.ID;
OID privProtocol = PrivDES.ID;
OctetString authPassphrase = new OctetString("masked");
OctetString privPassphrase = new OctetString("masked");
snmp.getUSM().addUser(securityName, new UsmUser(securityName, authProtocol, authPassphrase, privProtocol, privPassphrase));
UserTarget target = new UserTarget();
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setSecurityName(securityName);
target.setAddress(GenericAddress.parse(String.format("udp:%s/%s", "192.168.60.105", "161")));
target.setVersion(SnmpConstants.version3);
target.setRetries(2);
target.setTimeout(60000);
transport.listen();
PDU pdu = new ScopedPDU();
pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.27675.20.10.1.2.0")));
pdu.setType(PDU.GET);
ResponseEvent event = snmp.send(pdu, target);
if (event != null) {
PDU pdu2 = event.getResponse();
System.out.println(pdu2.get(0).getVariable().toString());
if (pdu2.getErrorStatus() == PDU.noError) {
System.out.println("SNMPv3 GET Successful!");
} else {
System.out.println("SNMPv3 GET Unsuccessful.");
}
} else {
System.out.println("SNMP get unsuccessful.");
}
}
https://stackoverflow.com/questions/55362602
复制相似问题