snmp getTable demo :iftable ipAddresstable

package org.huangxf.snmp.test;

import java.io.IOException;
import java.util.List;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.TransportMapping;
import org.snmp4j.mp.SnmpConstants;
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;
import org.snmp4j.util.PDUFactory;
import org.snmp4j.util.TableEvent;
import org.snmp4j.util.TableUtils;

public class ipTable {
    public static void main(String[] args) {
        ipTable.collectInterface();
    }

    // 服务器接口集合
    public static void collectInterface() {
        TransportMapping transport = null;
        Snmp snmp = null;
        CommunityTarget target;
        String[] IF_OIDS = { "1.3.6.1.2.1.2.2.1.1", // Index
                "1.3.6.1.2.1.2.2.1.2", // descr
                "1.3.6.1.2.1.2.2.1.3", // type
                "1.3.6.1.2.1.2.2.1.5", // speed
                "1.3.6.1.2.1.2.2.1.6", // mac
                "1.3.6.1.2.1.2.2.1.7", // adminStatus
                "1.3.6.1.2.1.2.2.1.8", // operStatus

                "1.3.6.1.2.1.2.2.1.10", // inOctets
                "1.3.6.1.2.1.2.2.1.16", // outOctets
                "1.3.6.1.2.1.2.2.1.14", // inError
                "1.3.6.1.2.1.2.2.1.20", // outError
                "1.3.6.1.2.1.2.2.1.13", // inDiscard
                "1.3.6.1.2.1.2.2.1.19", // outDiscard
                "1.3.6.1.2.1.2.2.1.11", // inUcastPkts
                "1.3.6.1.2.1.2.2.1.17", // outUcastPkts
                "1.3.6.1.2.1.2.2.1.12", // inNUcastPkts
                "1.3.6.1.2.1.2.2.1.18" };// outNUcastPkts
        String[] IP_OIDS = { "1.3.6.1.2.1.4.20.1.1", // ipAdEntAddr
                "1.3.6.1.2.1.4.20.1.2", // ipAdEntIfIndex
                "1.3.6.1.2.1.4.20.1.3" ,// ipAdEntNetMask
                "1.3.6.1.2.1.4.20.1.4" ,//ipAdentBcastAddr
                "1.3.6.1.2.1.4.20.1.5" };//ipAdEntReasmMaxSize
        try {
            transport = new DefaultUdpTransportMapping();
            snmp = new Snmp(transport);
            snmp.listen();
            target = new CommunityTarget();
            target.setCommunity(new OctetString("public"));
            target.setRetries(2);
            target.setAddress(GenericAddress.parse("udp:127.0.0.1/161"));
            target.setTimeout(8000);
            target.setVersion(SnmpConstants.version2c);
            TableUtils tableUtils = new TableUtils(snmp, new PDUFactory() {
                @Override
                public PDU createPDU(Target arg0) {
                    PDU request = new PDU();
                    request.setType(PDU.GET);
                    return request;
                }
            });
            OID[] columns = new OID[IF_OIDS.length];
            for (int i = 0; i < IF_OIDS.length; i++)
                columns[i] = new OID(IF_OIDS[i]);
            @SuppressWarnings("unchecked")
            List<TableEvent> list = tableUtils.getTable(target, columns, null, null);
            if (list.size() == 1 && list.get(0).getColumns() == null) {
                System.out.println(" null");
            } else {
                for (TableEvent event : list) {
                    VariableBinding[] values = event.getColumns();
                    if (values == null)
                        continue;
                    System.out.println("interface ---Index:" + values[0].getVariable().toString() + "  descr:"
                         + "  type:"
                            + values[2].getVariable().toString() + " speed:" + values[3].getVariable().toString()
                            + " mac:" 
                            + values[5].getVariable().toString() + "  operStatus:"
                            + values[6].getVariable().toString());
//                    System.out.println("interface ---Index:" + values[0].getVariable().toString() + "  descr:"
//                            + getChinese(values[1].getVariable().toString()) + "  type:"
//                            + values[2].getVariable().toString() + " speed:" + values[3].getVariable().toString()
//                            + " mac:" + getChinese(values[4].getVariable().toString()) + " adminStatus:"
//                            + values[5].getVariable().toString() + "  operStatus:"
//                            + values[6].getVariable().toString());
                }
            }
            // 获取ip
            OID[] ipcolumns = new OID[IP_OIDS.length];
            for (int i = 0; i < IP_OIDS.length; i++)
                ipcolumns[i] = new OID(IP_OIDS[i]);
            @SuppressWarnings("unchecked")
            List<TableEvent> iplist = tableUtils.getTable(target, ipcolumns, null, null);
            if (iplist.size() == 1 && iplist.get(0).getColumns() == null) {
                System.out.println(" null");
            } else {
                for (TableEvent event : iplist) {
                    VariableBinding[] values = event.getColumns();
                    if (values == null)
                        continue;
                    System.out.println(" IP--->ipAdEntAddr:" + values[0].getVariable().toString() + "   ipAdEntIfIndex:"
                            + values[1].getVariable().toString() + "   ipAdEntNetMask:"
                            + values[2].getVariable().toString() + " 3:" + values[3].getVariable().toString()
                            + " 4:" 
                            + values[4].getVariable().toString() );
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (transport != null)
                    transport.close();
                if (snmp != null)
                    snmp.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

posted @ 2016-04-14 17:27  Alamps  阅读(1694)  评论(6编辑  收藏  举报