我一直在使用SNMP4J库在java的简单SNMP管理器中工作。我正在尝试发送简单的异步获取消息。我已经开始使用SNMP管理器创建PDU并启动监听器和一个名为SNMPget的简单类,它可以生成简单的get请求。
如javadoc中所述:
通过在实现ResponseListener接口的对象实例上调用回调方法来返回异步响应。
我已按照确切的说明实现回调,但看起来回调方法onResponse在发送get请求时从不执行。实际上,我已经使用wireshark检查是否正在发送数据包并且请求是否正确发送并且接收到正确的响应。关于我做错了什么的任何线索?
public class SNMPManager {
private static SNMPManager instance = new SNMPManager();
private Snmp snmp_ = null;
//Common SNMP variables
private int requestPort = 161;
private int timeout = 10000;
private int retryCount = 1;
private int maxRepetitions = 20;
private int nonRepeaters = 0;
//Singleton pattern
public static SNMPManager getInstance() {
return instance;
}
//Empty Constructor
private SNMPManager() { }
//Start the manager
public void start() throws IOException {
//Create a UDP transport on all interfaces
DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping();
//Create a SNMP instance
snmp_ = new Snmp(transport);
//Put all the transport mappings into listen mode
snmp_.listen();
}
//Methods for creating targets
// Create the target for version 2c
public Target createVersion2cTarget(String ipaddress, String communityName) {
CommunityTarget target = new CommunityTarget();
target.setAddress(new UdpAddress(String.format("%s/%d", ipaddress, requestPort)));
target.setCommunity(new OctetString(communityName));
target.setTimeout(timeout);
target.setRetries(retryCount);
target.setVersion(SnmpConstants.version2c);
return target;
}
//Methods for creating PDUs
public PDU createGetRequestPdu(String requestOID) {
PDU pdu = new PDU();
//Set the request type
pdu.setType(PDU.GET);
//Set the OID
OID rOID = new OID(requestOID);
pdu.add(new VariableBinding(rOID));
return pdu;
}
//Methods for the request (async mode)
public void getRequest(PDU pdu, Target target, Object userHandle, ResponseListener listener) throws IOException {
snmp_.get(pdu, target, userHandle, listener);
}
}
public class SNMPget {
protected final SNMPManager snmp_;
private String requestOID;
private String ipAddress;
private String communityName;
public SNMPget(String newRequestOID, String hostIpAddress, String newCommunityName) {
snmp_ = SNMPManager.getInstance();
requestOID = newRequestOID;
ipAddress = hostIpAddress;
communityName = newCommunityName;
}
public void get(ResponseListener listener) throws IOException {
Target targetHost = snmp_.createVersion2cTarget(ipAddress, communityName);
PDU pduSNMPget = snmp_.createGetRequestPdu(requestOID);
snmp_.getRequest(pduSNMPget, targetHost, null, listener);
}
}
public class mainTest {
public static void main(String[] args) throws IOException {
SNMPManager snmp = SNMPManager.getInstance();
snmp.start();
ResponseListener listener = new ResponseListener() {
@Override
public void onResponse(ResponseEvent event) {
// Always cancel async request when response has been received
// otherwise a memory leak is created! Not canceling a request
// immediately can be useful when sending a request to a broadcast
// address.
((Snmp) event.getSource()).cancel(event.getRequest(), this);
PDU response = event.getResponse();
System.out.println("Received response "+response);
}
};
SNMPget snmpGetCmd = new SNMPget("1.3.6.1.2.1.1.1.0", "192.168.137.104", "public");
snmpGetCmd.get(listener);
}
}