SNMP映射MAC地址交换机端口(SNMP Mapping Mac Address to Switc

2019-10-22 10:12发布

我在一个数据中心的工作,我在写一个PHP的工具,它映射我们所有的设备,能告诉我们,如果有什么外面有什么被标榜为的过程。

它首先拉一个巨大从两个核心的Mac和其IPS的列表到一个临时表。 然后,它遍历所有机架的*,并试图找出哪一个端口该MAC属于。 由于没有黄金命令(在你的头上提示灯泡),我必须:

  1. 创建一个多阵列与端口作为密钥和该值的索引序号。
  2. 与网桥ID替换的ifIndex。
  3. 更换与MAC哈希网桥ID。
  4. Repalce的MAC哈希的实际MAC

最后,它需要的MAC,IPS和端口并填充主表。

问题是第一步。 1.3.6.1.2.1.31.1.1.1.1适用于大多数的交换机但也有一些foundrys不工作。 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)?

任何对此的方向将是巨大的。 -Justin

* =架型号:思科2900XL,铸造FI4802 +变种

Answer 1:

你可以这样做(在惠普的Procurve测试):

从Linux服务器:

$运行snmpwalk -v 1 -c公共xxx.xxx.xxx.xxx 1.3.6.1.2.1.17.4.3.1.2 | grep的 “INTEGER:11”

(端口编号11)

将返回 :

的SNMPv2-SMI :: NEI 2.17.4.3.1.2.44.118.138.64.143.95 = INTEGER:11的SNMPv2-SMI :: NEI 2.17.4.3.1.2.56.170.60.108.174.57 = INTEGER:11的SNMPv2 SMI :: NEI -2.17.4.3.1.2.104.181.153.172.54.237 = INTEGER:11的SNMPv2-SMI :: NEI 2.17.4.3.1.2.120.172.192.143.226.236 = INTEGER:11的SNMPv2-SMI :: NEI 2.17.4.3.1.2 .124.195.161.20.109.76 = INTEGER:11的SNMPv2-SMI :: NEI 2.17.4.3.1.2。 152.75.225.59.127.180 = INTEGER:11

然后,你可以做到这一点找哪个MAC地址连接:

$运行snmpwalk -v 1 -c公共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 =十六进制字符串: 98 4B 3B E1 7F B4

你可以做一个script.sh做到这一点...



Answer 2:

当我需要发现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或任何



Answer 3:

如果您想任何一个从Windows Server这样做编程,你可以使用SnmpSharpNet库来完成同样的事情。 下面是将使用OID 1.3.6.1.2.1.17.7.1.2.2.1.2.1创建一个特定的戴尔交换机上的所有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();
    }

有利用这个例子可解决此信息反馈给主机名的整个项目在这里



文章来源: SNMP Mapping Mac Address to Switch Port
标签: php snmp