SNMP将Mac地址映射到交换机端口

时间:2010-05-28 17:36:34

标签: php snmp

我在一个数据中心工作,我正在编写一个映射我们所有设备的php工具,并且可以告诉我们那里有什么是收费的。

它首先将巨大的mac及其ips列表从两个内核中拉入临时表。然后,它遍历所有机架*并尝试查找mac所属的端口。由于没有黄金指令(头上有灯泡),我必须:

  1. 使用端口作为键创建一个多数组,并使用ifindex作为值。
  2. 将ifindex替换为桥ID。
  3. 将网桥ID替换为mac hash。
  4. 使用实际的mac
  5. 重新生成mac哈希

    最后,它需要mac,ips和port并填充主表。

    问题在于第一步。 1.3.6.1.2.1.31.1.1.1.1适用于大多数交换机,但有一些不起作用。 1.3.6.1.4.1.1991.1.1.3.3.1.1.38接近我正在寻找的东西,但我并不完全舒服,这正是我正在寻找的。我能够找到铸造厂下的特定设备型号>产品>注册,但该文件夹下没有任何MIB。所以我的问题是:

    1. 是否有代工厂特定的字符串返回端口和mac? ifindexes也可以。
    2. 如何使用设备特定的MIB(enterprises.foundry.products.registration.snFWSXFamily)?
    3. 任何方向都会很棒。 -Justin

      * =机架型号:cisco 2900xl,代工厂FI4802 +变种

3 个答案:

答案 0 :(得分:2)

您可以这样做(在HP Procurve上测试):

从您的Linux服务器:

  

$ snmpwalk -v 1 -c public xxx.xxx.xxx.xxx 1.3.6.1.2.1.17.4.3.1.2 | grep的   “整数:11”

(端口号11)

将返回:

  

SNMPv2-SMI :: mib-2.17.4.3.1.2.44.118.138.64.143.95 = INTEGER:11   SNMPv2-SMI :: mib-2.17.4.3.1.2.56.170.60.108.174.57 = INTEGER:11   SNMPv2-SMI :: mib-2.17.4.3.1.2.104.181.153.172.54.237 = INTEGER:11   SNMPv2-SMI :: mib-2.17.4.3.1.2.120.172.192.143.226.236 = INTEGER:11   SNMPv2-SMI :: mib-2.17.4.3.1.2.124.195.161.20.109.76 = INTEGER:11   SNMPv2-SMI :: mib-2.17.4.3.1.2。 152.75.225.59.127.180 = INTEGER:11

然后,您可以执行此操作以查找已连接的Mac地址:

  

$ snmpwalk -v 1 -c public xxx.xxx.xxx.xxx 1.3.6.1.2.1.17.4.3.1.1 |   grep“152.75.225.59.127.180”

返回mac地址:

  

SNMPv2-SMI :: mib-2.17.4.3.1.1.152.75.225.59.127.180 = Hex-STRING: 98   4B E1 3B 7F B4

您可以创建一个script.sh来执行此操作...

答案 1 :(得分:0)

当我需要从交换机发现MAC和其他信息时,我使用'snmpwalk'和'snmpbulkwalk'命令来检查他们的SNMP数据内容

例如:

snmpbulkwalk -v2c 192.168.30.40 -c public 1.3.6.1.2.1.31.1.1.1.1

输出:

IF-MIB::ifName.1 = STRING: Gi0/1
IF-MIB::ifName.2 = STRING: Gi0/2
IF-MIB::ifName.3 = STRING: Gi0/3
IF-MIB::ifName.4 = STRING: Gi0/4
IF-MIB::ifName.5 = STRING: Gi0/5
IF-MIB::ifName.6 = STRING: Gi0/6
IF-MIB::ifName.7 = STRING: Gi0/7
IF-MIB::ifName.8 = STRING: Gi0/8
IF-MIB::ifName.9 = STRING: Gi0/9
IF-MIB::ifName.10 = STRING: Gi0/10
IF-MIB::ifName.11 = STRING: Gi0/11
IF-MIB::ifName.12 = STRING: Gi0/12
IF-MIB::ifName.13 = STRING: Nu0
IF-MIB::ifName.14 = STRING: Vl1
IF-MIB::ifName.15 = STRING: Vl2
IF-MIB::ifName.16 = STRING: Vl416

snmpbulkwalk -v2c 192.168.30.40 -c public 1.3.6.1.2

输出大量信息,您可以在其中查找自己喜欢的MAC或任何其他内容

答案 2 :(得分:0)

如果您希望以编程方式从Windows Server执行此操作,则可以使用SnmpSharpNet库来完成相同的操作。下面是一个示例,它将使用OID 1.3.6.1.2.1.17.7.1.2.2.1.2.1

创建特定Dell交换机上所有MAC地址和端口的列表
    using SnmpSharpNet;

    List<KeyValuePair<string, string>> portList = new List<KeyValuePair<string, string>>();

    IPAddress ip = IPAddress.Parse("192.168.0.2");
    SnmpWalk(ip, "snmpcommunity", "1.3.6.1.2.1.17.7.1.2.2.1.2.1", "1");

    //SNMPWALK the ports on a switch or stack of switches. Ports will be labeled SwitchNum/Stack Number/Port Numbers.
    private void SnmpWalk(IPAddress ip, string snmpCommunity, string oid, string switchNum)
    {
        UdpTarget target = new UdpTarget(ip);

        // SNMP community name
        OctetString community = new OctetString(snmpCommunity);
        // Define agent parameters class
        AgentParameters param = new AgentParameters(community);
        // Set SNMP version to 1
        param.Version = SnmpVersion.Ver1;


        // Define Oid that is the root of the MIB tree you wish to retrieve
        Oid rootOid = new Oid(oid);

        // This Oid represents last Oid returned by the SNMP agent
        Oid lastOid = (Oid)rootOid.Clone();

        // Pdu class used for all requests
        Pdu pdu = new Pdu(PduType.GetNext);

        // Loop through results
        while (lastOid != null)
        {
            // When Pdu class is first constructed, RequestId is set to a random value
            // that needs to be incremented on subsequent requests made using the
            // same instance of the Pdu class.
            if (pdu.RequestId != 0)
            {
                pdu.RequestId += 1;
            }
            // Clear Oids from the Pdu class.
            pdu.VbList.Clear();
            // Initialize request PDU with the last retrieved Oid
            pdu.VbList.Add(lastOid);
            // Make SNMP request
            SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);
            // You should catch exceptions in the Request if using in real application.

            // If result is null then agent didn't reply or we couldn't parse the reply.
            if (result != null)
            {
                // ErrorStatus other then 0 is an error returned by 
                // the Agent - see SnmpConstants for error definitions
                if (result.Pdu.ErrorStatus != 0)
                {
                    // agent reported an error with the request
                    Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
                        result.Pdu.ErrorStatus,
                        result.Pdu.ErrorIndex);
                    lastOid = null;
                    break;
                }
                else
                {
                    // Walk through returned variable bindings
                    foreach (Vb v in result.Pdu.VbList)
                    {
                        // Check that retrieved Oid is "child" of the root OID
                        if (rootOid.IsRootOf(v.Oid))
                        {
                            //Convert OID to MAC
                            string[] macs = v.Oid.ToString().Split('.');
                            string mac = "";
                            int counter = 0;
                            foreach (string chunk in macs)
                            {
                                if (counter >= macs.Length - 6)
                                {
                                    mac += string.Format("{0:X2}", int.Parse(chunk));
                                }
                                counter += 1;
                            }

                            //Assumes a 48 port switch (52 actual). You need to know these values to correctly iterate through a stack of switches.
                            int dellSwitch = 1 + int.Parse(v.Value.ToString()) / 52;
                            int port = int.Parse(v.Value.ToString()) - (52 * (dellSwitch - 1));
                            KeyValuePair<string, string> Port = new KeyValuePair<string, string>(mac, switchNum + "/" + dellSwitch.ToString() + "/" + port.ToString());
                            portList.Add(Port);

                            //Exit Loop
                            lastOid = v.Oid;
                        }
                        else
                        {
                            //End of the requested MIB tree. Set lastOid to null and exit loop
                            lastOid = null;
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("No response received from SNMP agent.");
            }
        }
        target.Close();
    }

有一个整个项目利用此示例将此信息解析回主机名here

相关问题