public class RouterTable {
public RouterTable(){
}
public synchronized static String collector(String ip){
CommunityTarget target=new CommunityTarget();
target.setAddress(new UdpAddress(ip+"/161"));
target.setRetries(1);
target.setTimeout(5000);
target.setVersion(SnmpConstants.version2c);
target.setCommunity(new OctetString("public"));
PDU pdu = new PDU();
String[] oid={"1.3.6.1.2.1.4.21.1.1","1.3.6.1.2.1.4.21.1.7","1.3.6.1.2.1.4.21.1.8","1.3.6.1.2.1.4.21.1.11","1.3.6.1.2.1.4.21.1.3"};
String[] value=new String[oid.length];
StringBuffer sb = new StringBuffer();
sb.append("目的ip");
sb.append(Constants.COL_SEPRATOR);
sb.append("下一跳路由ip");
sb.append(Constants.COL_SEPRATOR);
sb.append("路由类型");
sb.append(Constants.COL_SEPRATOR);
sb.append("子网掩码");
sb.append(Constants.COL_SEPRATOR);
sb.append("主metric");
sb.append(Constants.COL_SEPRATOR);
sb.append(Constants.LINE_SEPERATOR);
for(String s="1.3.6.1.2.1.4.21.1.1";s.equals("1.3.6.1.2.1.4.21.1.1");s=oid[0].substring(0, 20))
{
for(int k=0;k<oid.length;k++){
pdu.add(new VariableBinding(new OID(oid[k])));
// System.out.println(s);
}
pdu.setType(PDU.GETNEXT);
try{
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
ResponseEvent respEvent = snmp.send(pdu, target);
if (respEvent != null && respEvent.getResponse() != null) {
PDU pduResponse=respEvent.getResponse();
Vector vector=pduResponse.getVariableBindings();
String[] string2=new String[oid.length];
for(int l=0;l<vector.size();l++){
string2[l]=vector.get(l).toString();
oid[l]=string2[l].substring(0,string2[l].indexOf("=")-1).trim();
value[l]=string2[l].substring(string2[l].indexOf("=")+2, string2[l].length()).trim();
if ((oid[l].charAt(19)+"").equals("2")){
break;
}
sb.append(value[l]);
sb.append(Constants.COL_SEPRATOR);
//System.out.println(oid[l]);
//System.out.println(value[l]);
pdu.remove(0);
}
sb.append(Constants.LINE_SEPERATOR);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
//System.out.println(sb.toString());
return sb.toString();
}
}
循环条件的设置