【问题标题】:Find Neighbour with SNMP LLDP使用 SNMP LLDP 查找邻居
【发布时间】:2013-03-31 15:46:04
【问题描述】:

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

非常感谢您的帮助!!!

【问题讨论】:

  • 现在我找到了 MIB-Tree lldpObjects (1.0.8802.1.1.2.1)。里面有 lldpRemoteSystemsData (1.0.8802.1.1.2.1.3),它包含当前交换机上的端口。然后我找到了 lldpRemoteSystemsData (1.0.8802.1.1.2.1.4),其中包含邻居的端口和 MAC 地址。但我的问题是找到当前交换机上的哪个端口连接了邻居?他们是不是一个转换表来查看当前交换机的哪个端口连接到mac地址为xxx的邻居交换机的端口
  • 我也面临这个问题,我需要获取远程IpAddress,但是它给出了MAC地址,我们如何从LLDP MIB获取远程设备IpAddress,请帮助...

标签: network-programming ip snmp network-monitoring


【解决方案1】:

此代码在包含 ip 地址的列表中的设备上遍历 LLDP oid,并将邻居添加到并行阵列。 前任; (每个标题都是ArrayList) 主机名 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();*/


    }

}

【讨论】:

    【解决方案2】:

    我认为您需要 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,它应该映射到设备上的正确接口。这有帮助吗?

    【讨论】:

    • 感谢您的回答。但我不确定这些信息。你能给我一个例子吗? MIB 1.0.8802.1.1.2.1.4.1.1.5 为我提供了来自邻居的 MAC 地址,因此无需获取与当前交换机连接的端口号。 (不是邻居)
    • 另一个问题:当我拥有设备的 MAC 地址时,是否有可能获得 IP_Address? (注:我只有网络中的交换机)
    • 当然。所以假设你有这个人:
    • LLDP-MIB::lldpLocPortId.4 = STRING: "FastEthernet1" 。那么您还应该拥有其中之一:IF-MIB::ifName.4 = STRING: "eth1"。您将 lldpLocPortNum 与 ifIndex 交叉引用,即上面的“4”。如果您需要,我可以添加更多详细信息,SNMP 可能很神秘。
    • 这也适用于 lldpRemLocalPortNum(它与 LldpPortNumber 类型相同)。
    【解决方案3】:

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

    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)"
    

    这就是你进行映射的方式。

    【讨论】:

    • 所以当我查看:lldpRemTable.lldpRemEntry.lldpRemChassisId.1325376015.2.1 ec e5 55 32 20 88 时,数字 .2 总是给我当前交换机的端口号,该交换机连接到该 mac地址?您是否有一些很好的资源,其中解释了更多关于 snmp 的详细信息,例如我们讨论的内容?
    • 另一个问题:当我拥有设备的 MAC 地址时,是否有可能获得 IP_Address? (注:我只有网络中的交换机)
    • 是的;他们总是给你端口号。请注意,端口 number 是任意的;谁是 1 号端口? eth1?瞧? eth_br? (看到问题了吗?)如果没有从 2(端口号)到描述(ifName = "eth2" 等)的映射,端口号就没有意义。但是有了映射,它就非常有用了。
    • 保证你可以得到IP地址;也许遥控器没有IP地址,对吧?lldpRemTable.lldpRemEntry.lldpRemChassisId.1325376015.2.1 ec e5 55 32 20 88表示遥控器的ChassisID是MAC地址。但是来自远程的 LLDP 帧中的 ChassisID 可以是 IP 地址、MAC 地址或其他任何内容(请参阅 lldpRemChassisIdSubtype)。获得 IP 地址是一个不错的奖励,但您不能依赖它; LLDP 是第 2 层协议,因此 IP(第 3 层)高于其付费等级。
    • 我如何从邻居那里获取 IP 地址?有没有好的解决方案来获取 IP 地址?
    猜你喜欢
    • 1970-01-01
    • 2020-01-29
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多