我正在尝试使用SNMP4J(v2.6.2)查询(snmpget)两个不同的目标
机器一次。我尝试了两种方法,而且总是
收到第一个目标的响应。我问哪个
第二永远不会得到回应。 (我可以从
命令行就可以了,包括单独和同时进行。)
我尝试的方法使用一个Snmp
,一个Transport
和两个UserTarget
。
另一种方法使用两个Snmp
,两个Transport
和两个
UserTarget
个。它们的行为相同。我也得到同样的效果
如果我发送一个请求,请等到该请求完成后再发送
第二个。
我写了一个最小的示例并将其粘贴到下面。也许有人 可以给我提示我做错了什么。谢谢!
package snmpclienttext;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
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.event.ResponseListener;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.AuthSHA;
import org.snmp4j.security.PrivAES;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.TSM;
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.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
public class SNMPClientTest {
static class MySNMP {
TransportMapping<? extends Address> transport;
Snmp snmp;
OctetString securityName;
static String password = "temppassword";
UserTarget target;
void setup(String ip) throws IOException {
DefaultUdpTransportMapping d = new DefaultUdpTransportMapping();
d.setReceiveBufferSize(65536);
transport = d;
System.out.println("Listen address: " +
transport.getListenAddress());
transport.listen();
snmp = new Snmp(transport);
OctetString localEngineId = new
OctetString(MPv3.createLocalEngineID());
USM usm = new USM(SecurityProtocols.getInstance(),
localEngineId, 0);
SecurityModels.getInstance().addSecurityModel(usm);
securityName = new OctetString("SHA");
OID authProtocol = AuthSHA.ID;
OID privProtocol = org.snmp4j.security.PrivAES128.ID;
System.out.println("snmpv3password: " + password);
OctetString authPassphrase = new OctetString(password);
OctetString privPassphrase = new OctetString(password);
snmp.getUSM().addUser(securityName, new
UsmUser(securityName, authProtocol, authPassphrase, privProtocol,
privPassphrase));
SecurityModels.getInstance().addSecurityModel(new
TSM(localEngineId, false));
target = new UserTarget();
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setSecurityName(securityName);
target.setAddress(GenericAddress.parse(String.format("udp:%s/%s",
ip, "161")));
target.setVersion(SnmpConstants.version3);
target.setRetries(0);
target.setTimeout(60000);
}
public void readRequest(String oid) throws IOException {
PDU pdu = new ScopedPDU();
pdu.add(new VariableBinding(new OID(oid)));
pdu.setType(PDU.GET);
try {
System.out.println("snmp get for " + target.getAddress());
snmp.get(pdu, target, null, listener);
} catch (IOException ex) {
System.out.println("SNMP Fail");
return;
}
}
}
static ResponseListener listener = new ResponseListener() {
@Override
public void onResponse(ResponseEvent event) {
if (event != null) {
Address addr = event.getPeerAddress();
if (addr == null) {
System.out.println("Null address");
return;
}
System.out.println("Peer Address = " + addr);
PDU rpdu = event.getResponse();
if (rpdu != null) {
if (rpdu.getErrorStatus() == PDU.noError) {
for (VariableBinding vb : rpdu.getVariableBindings()) {
OID oid = vb.getOid();
org.snmp4j.smi.Variable var = vb.getVariable();
String val = var.toString();
if (var.isException()) val = "";
System.out.println("Binding for " + oid +
" has " + vb.toString().length() + " characters, var has " +
val.length() + " characters");
}
} else {
System.out.println("Snmp Get Unsuccessful.");
}
} else {
System.out.println("Snmp response is null.");
}
} else {
System.out.println("SNMP event null.");
}
}
};
public static void main(String[] args) throws IOException,
InterruptedException {
MySNMP a = new MySNMP();
MySNMP b = new MySNMP();
a.setup("10.1.2.78");
b.setup("10.1.2.135");
a.readRequest("1.3.6.1.4.1.6713.1.3.1.2.1.1.50");
Thread.sleep(10000);
b.readRequest("1.3.6.1.4.1.6713.1.3.1.2.1.1.50");
Thread.sleep(10000);
}
}