snmp4j获取表

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();

 
 }
 }

循环条件的设置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值