使用SNMP LLDP查找邻居

时间:2013-04-09 09:20:17

标签: network-programming ip snmp network-monitoring

我有几个连接在一起的开关(Hirschmann Mach1040,RS40)。所以我想用snmp触发端口x上连接的neigbour交换机的mac地址/ ip地址。是否有一个lldp mib_value告诉我在swich上的特定端口的neihbour mac或ip ??

非常感谢你的帮助!!!

3 个答案:

答案 0 :(得分:1)

此代码在列表中的设备上传输LLDP oid,其中包含ip addres并将neighbourship添加到并行阵列。 前; (每个标题都是ArrayList) HostName HostInterface NeigbourName NeighbourInt sw2960 fa0 / 1 HP-98765 Ethernet0 / 4

   package NDMSMain;

import java.io.IOException;
import java.util.ArrayList;

import SNMPHandler.SNMPManager;
import SNMPHandler.SNMPWalk;
import TopologyHandler.WriteXMLFile;

public class LldpDiscovery {

    static SNMPWalk t1 = new SNMPWalk();
    public static String LLDP_OID = "1.0.8802.1.1.2.1.4.1.1";
    public static String snmpCheck = null; //MODEL
    public static String devicePid = null;  //Product Id

    public static ArrayList<String> topologyNodeId = new ArrayList<String>();
    public static ArrayList<String> lldpHostName = new ArrayList<String>();
    public static ArrayList<String> lldpHostInt = new ArrayList<String>();
    public static ArrayList<String> lldpNeigName = new ArrayList<String>();
    public static ArrayList<String> lldpNeigInt = new ArrayList<String>();
    public static ArrayList<String> lldpNeigMac = new ArrayList<String>();
    public static ArrayList<String> deviceModel = new ArrayList<String>();


    public static void main() {

        System.out.println("LLDP GELDIIIII");

        frame1 out = new frame1();
        String testWalk="";

        String satir[]=null;
        String esit[]=null;
        String hostInt = null;
        for(int i=0; i<out.ipler.size();i++)
        {
            try {

                devicePid = SNMPManager.main("udp:"+out.ipler.get(i).toString()+"/161","1.0.8802.1.1.2.1.5.4795.1.2.7.0");

            if(!devicePid.startsWith("no"))
            {
                // To determine node count and Id s

                snmpCheck= SNMPManager.main("udp:"+out.ipler.get(i).toString()+"/161","1.3.6.1.2.1.1.5.0");
                deviceModel.add(devicePid);
                topologyNodeId.add(snmpCheck);



            testWalk =  t1.main(out.ipler.get(i).toString(),LLDP_OID);


            satir= testWalk.split("\n");
            //System.out.println("satir 0"+satir[0].toString());
            //System.out.println("satir 1"+satir[1].toString());
            int ind =0;
            for(int k=0;k<satir.length;k++)
            {

                esit = null;
                esit = satir[k].split("=");
                //System.out.println("===Hostlar");
                //System.out.println("ind 0: "+esit[ind]);
                if(esit[ind].startsWith("1.0.8802.1.1.2.1.4.1.1.7.0") && !esit[ind].equals("1.0.8802.1.1.2.1.4.1.1.7.0.1.2 "))
                {


                    hostInt = esit[ind].substring(esit[ind].length()-5, esit[ind].length()-3);
                    //System.out.println("===Start-4.1.1.7.0 - genel cnt: "+ arrayCnts +" fa0-"+hostInt);
                    if(!hostInt.startsWith("."))
                    {
                        //System.out.println("===Start-4.1.1.7.0 - .li cnt: " + arrayCnts +"fa0-"+hostInt);
                        lldpHostInt.add("fa0-"+hostInt);

                    }
                    else
                    {
                        //System.out.println("===Start-4.1.1.7.0 - .siz cnt: " + arrayCnts +"fa0-"+hostInt);
                        lldpHostInt.add("fa0-"+hostInt.substring(hostInt.length()-1,hostInt.length())); 

                    }

                    //System.out.println("=== LLDPNeigh cnt:" + arrayCnts + esit[1]);
                    lldpNeigInt.add(esit[1]);


                }
                else if(esit[ind].startsWith("1.0.8802.1.1.2.1.4.1.1.9.0"))
                {
                    //System.out.println("=== NeighNAme cnt:" + arrayCnts + esit[1]);
                    if(!esit[1].equals(" "))
                    {
                        lldpNeigName.add(esit[1]);
                        lldpHostName.add(snmpCheck);
                    }



                //  lldpHostName.add(out.asd);

                }
                else if(esit[ind].startsWith("1.0.8802.1.1.2.1.4.1.1.5.0") && !esit[ind].equals("1.0.8802.1.1.2.1.4.1.1.5.0.1.2 "))
                {

                    lldpNeigMac.add(esit[1]);
                }
            }
            }

            else   // SNMP DESTEKLEMEYEN CIHAZLAR ICIN COZUM BULUNACAK
            {


                System.out.println("*********There is unknown device here!!!! ******");

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }//
        }


        System.out.println("---HostName---");
        for(int i=0; i<lldpHostName.size();i++)
        {
            System.out.println(lldpHostName.get(i).toString());
        }
        System.out.println("---HostInt---");
        for(int i=0; i<lldpHostInt.size();i++)
        {

            System.out.println(lldpHostInt.get(i).toString());

        }
        System.out.println("---NeighInt---");
        for(int i=0; i<lldpHostInt.size();i++)
        {

            System.out.println(lldpNeigInt.get(i).toString());

        }
        System.out.println("---NeighName---");
        for(int i=0; i<lldpNeigName.size();i++)
        {
            System.out.println(lldpNeigName.get(i).toString());
        }
        System.out.println("---NeighMac---");
        for(int i=0; i<lldpHostInt.size();i++)
        {
            System.out.println(lldpNeigMac.get(i).toString());
        }
        System.out.println("---NodeId---");
        for(int i=0; i<topologyNodeId.size();i++)
        {
            System.out.println(topologyNodeId.get(i).toString());
        }

        for (int i=0;i<lldpNeigName.size();i++)
        {
            if(!topologyNodeId.contains(lldpNeigName.get(i).trim()))
            {
                topologyNodeId.add(lldpNeigName.get(i));
            }
        }


        System.out.println("===LLDPNODE ID");
        for(int i=0; i<topologyNodeId.size();i++)
        {
            System.out.println(topologyNodeId.get(i).toString());
        }   




    //  createCSV();

        //WriteXMLFile.main();



    enter code here

        /*lldpHostName.clear();
        lldpHostInt.clear();
        lldpNeigInt.clear();
        lldpNeigMac.clear();
        lldpNeigName.clear();
        topologyNodeId.clear();
        deviceModel.clear();*/


    }

}

答案 1 :(得分:0)

我认为你想要lldpRemLocalPortNum,它的语法类型为LldpPortNumber。 LldpPortNumber的定义是:

        A port number has no mandatory relationship to an
        InterfaceIndex object (of the interfaces MIB, IETF RFC 2863).
        If the LLDP agent is a IEEE 802.1D, IEEE 802.1Q bridge, the
        LldpPortNumber will have the same value as the dot1dBasePort
        object (defined in IETF RFC 1493) associated corresponding
        bridge port.  If the system hosting LLDP agent is not an
        IEEE 802.1D or an IEEE 802.1Q bridge, the LldpPortNumber
        will have the same value as the corresponding interface's
        **InterfaceIndex** object.

InterfaceIndex是IF-MIB :: ifIndex,它应映射到设备上的正确接口。这有帮助吗?

答案 2 :(得分:0)

所以这里有更多细节(我需要大文本框)。我们假设您有上述评论中提到的条目:

lldpRemTable.lldpRemEntry.lldpRemChassisId.1325376015.2.1 ec e5 55 32 20 88

请注意,lldpRemTable的索引为:

    INDEX   {
       lldpRemTimeMark,
       lldpRemLocalPortNum,
       lldpRemIndex
    }

在上面的示例中,您要求远程输入lldpRemChassisId(远程机箱ID)(他的机箱mac,ec e5 55 32 20 88)。 1325376015是lldpRemTimeMark表索引(发现远程时的时间值),2是lldpRemLocalPortNum表索引(您在远程上发现的本地端口号)。

lldpRemLocalPortNum是LldpPortNum的类型/文本约定。所以你也应该在lldpLocPortTable(也是LldpPortNum的类型)中有一个条目,lldpLocPortNum == 2(可能是IF-MIB :: ifIndex == 2)。因此lldpLocPortTable.lldpLocPortDesc.2将是您发现远程端口的端口的端口描述。即:

lldpLocPortTable.lldpLocPortDesc.2 = "FastEthernet2 (eth2)"

你是如何进行映射的。