【发布时间】:2015-02-05 21:27:28
【问题描述】:
我正在使用这个 SNMP4J 代码来执行一些 SNMP walks。但是,当我在 1.3.6.1.2.1.31.1.1.1.1(即 ifName)上运行它时,它会获取由 1.3.6.1.2.1.31.1.1.1.1.x 表示的所有接口,但随后它还抓取 1.3.6.1.2.1.31.1.1.1.2,它们是 ifInMulticastPkts,有时是 1.3.6.1.2.1.31.1.1.1.3,它们是 ifInBroadcastPkts。我只对 ifName 感兴趣。
如何防止 GETBULK 在遍历 MIB 之前递增最后一位?
public ArrayList<String> walk(String oid) throws IOException, InterruptedException {
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
Address targetAddress = GenericAddress.parse(address);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setVersion(SnmpConstants.version2c);
target.setAddress(targetAddress);
target.setTimeout(3000); //3s
target.setRetries(1);
PDU pdu = new PDU();
pdu.setType(PDU.GETBULK);
pdu.setMaxRepetitions(200);
pdu.setNonRepeaters(0);
pdu.add(new VariableBinding(new OID(oid)));
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
ArrayList<String> responsePieces = new ArrayList<String>();
if (response == null) {
System.out.println("TimeOut...");
}
else
{
if (response.getErrorStatus() == PDU.noError)
{
Vector<? extends VariableBinding> vbs = response.getVariableBindings();
for (VariableBinding vb : vbs) {
responsePieces.add(vb.toString());
}
}
else
{
System.out.println("Error:" + response.getErrorStatusText());
}
}
return responsePieces;
}
【问题讨论】: