首页 新闻 赞助 找找看

有人用SnmpSharpNet 获取路由器的mac地址吗?

0
[已关闭问题] 关闭于 2013-12-07 19:32

我想用SnmpSharpNet 获取路由器的mac地址,现在只能获取到sysDescr等叶子节点的信息,如何获得mac地址啊?

请各位大侠帮忙~~

  private void button1_Click(object sender, EventArgs e)

复制代码
        {
            
// SNMP community name
            OctetString community = new OctetString("public");

            AgentParameters param 
= new AgentParameters(community);
            param.Version 
= SnmpVersion.Ver1;
            IpAddress agent 
= new IpAddress("192.168.0.50");

            
// Construct target
            UdpTarget target = new UdpTarget((IPAddress)agent, 16120001);

            
// Pdu class used for all requests
            Pdu pdu = new Pdu(PduType.Get);
            pdu.VbList.Add(
"1.3.6.1.2.1.1.1.0"); //sysDescr
            pdu.VbList.Add("1.3.6.1.2.1.1.2.0"); //sysObjectID
            pdu.VbList.Add("1.3.6.1.2.1.1.3.0"); //sysUpTime
            pdu.VbList.Add("1.3.6.1.2.1.1.4.0"); //sysContact
            pdu.VbList.Add("1.3.6.1.2.1.1.5.0"); //sysName
       pdu.VbList.Add("1.3.6.1.2.1.3.1.1.2"); //这里想获取路由器的mac,但是不成功

            
// Make SNMP request
            SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);

            
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
                    this.txtContent.Text += string.Format("Error in SNMP reply. Error {0} index {1} \r\n",
                        result.Pdu.ErrorStatus,
                        result.Pdu.ErrorIndex);
                }
                
else
                {
                    
// Reply variables are returned in the same order as they were added
                    
//  to the VbList
                    this.txtContent.Text += string.Format("sysDescr({0}) ({1}): {2} \r\n",
                        result.Pdu.VbList[
0].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type),
                        result.Pdu.VbList[
0].Value.ToString());
                    
this.txtContent.Text += string.Format("sysObjectID({0}) ({1}): {2} \r\n",
                        result.Pdu.VbList[
1].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[1].Value.Type),
                        result.Pdu.VbList[
1].Value.ToString());
                    
this.txtContent.Text += string.Format("sysUpTime({0}) ({1}): {2} \r\n",
                        result.Pdu.VbList[
2].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[2].Value.Type),
                        result.Pdu.VbList[
2].Value.ToString());
                    
this.txtContent.Text += string.Format("sysContact({0}) ({1}): {2} \r\n",
                        result.Pdu.VbList[
3].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[3].Value.Type),
                        result.Pdu.VbList[
3].Value.ToString());
                    
this.txtContent.Text += string.Format("sysName({0}) ({1}): {2} \r\n",
                        result.Pdu.VbList[
4].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[4].Value.Type),
                        result.Pdu.VbList[
4].Value.ToString());
                }
            }
            
else
            {
                
this.txtContent.Text += string.Format("No response received from SNMP agent. \r\n");
            }
            target.Dispose();
        }
 
 
请问如何获取路由器的mac啊?
pdu.VbList.Add("1.3.6.1.2.1.3.1.1.2"); //这里想获取路由器的mac,但是不成功
c#
心语2012的主页 心语2012 | 菜鸟二级 | 园豆:216
提问于:2013-12-02 16:03
< >
分享
所有回答(1)
0

获取mac需要使用get-next读取,而上面的只能用get读取,所以错误

SNMP Version 1 GET-NEXT Example

using System;
using System.Net;
using SnmpSharpNet;

namespace sharpwalk
{
    class Program
    {
        static void Main(string[] args)
        {
            // SNMP community name
            OctetString community = new OctetString("public");
  // Define agent parameters class
            AgentParameters param = new AgentParameters(community);
            // Set SNMP version to 1
            param.Version = SnmpVersion.Ver1;
            // Construct the agent address object
            // IpAddress class is easy to use here because
            //  it will try to resolve constructor parameter if it doesn't
            //  parse to an IP address
            IpAddress agent = new IpAddress("127.0.0.1");
 
            // Construct target
            UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
 
            // Define Oid that is the root of the MIB
            //  tree you wish to retrieve
            Oid rootOid = new Oid("1.3.6.1.2.1.2.2.1.2"); // ifDescr
 
            // 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))
                            {
                                Console.WriteLine("{0} ({1}): {2}",
                                    v.Oid.ToString(),
                                    SnmpConstants.GetTypeName(v.Value.Type),
                                    v.Value.ToString());
                                lastOid = v.Oid;
                            }
                            else
                            {
                                // we have reached the 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 Version 2 GET-BULK Example

using System;
using System.Net;
using SnmpSharpNet;

namespace sharpwalk
{
    class Program
    {
        static void Main(string[] args)
        {
            // SNMP community name
            OctetString community = new OctetString("public");
 
            // Define agent parameters class
            AgentParameters param = new AgentParameters(community);
            // Set SNMP version to 2 (GET-BULK only works with SNMP ver 2 and 3)
            param.Version = SnmpVersion.Ver2;
            // Construct the agent address object
            // IpAddress class is easy to use here because
            //  it will try to resolve constructor parameter if it doesn't
            //  parse to an IP address
            IpAddress agent = new IpAddress("127.0.0.1");
 
            // Construct target
            UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
 
            // Define Oid that is the root of the MIB
            //  tree you wish to retrieve
            Oid rootOid = new Oid("1.3.6.1.2.1.2.2.1.2"); // ifDescr
 
            // 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.GetBulk);
 
            // In this example, set NonRepeaters value to 0
            pdu.NonRepeaters = 0;
            // MaxRepetitions tells the agent how many Oid/Value pairs to return
            // in the response.
            pdu.MaxRepetitions = 5;
 
            // Loop through results
            while (lastOid != null)
            {
                // When Pdu class is first constructed, RequestId is set to 0
                // and during encoding id will be set to the random value
                // for subsequent requests, id will be set to a value that
                // needs to be incremented to have unique request ids for each
                // packet
                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
                SnmpV2Packet result = (SnmpV2Packet)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))
                            {
                                Console.WriteLine("{0} ({1}): {2}",
                                    v.Oid.ToString(),
                                    SnmpConstants.GetTypeName(v.Value.Type),
                                    v.Value.ToString());
                                if (v.Value.Type == SnmpConstants.SMI_ENDOFMIBVIEW)
                                    lastOid = null;
                                else
                                    lastOid = v.Oid;
                            }
                            else
                            {
                                // we have reached the 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();
        }
    }
}

 

心语2012 | 园豆:216 (菜鸟二级) | 2013-12-04 17:40

获取交换机端口vlan id代码:

先要得到该设备上的vlan列表,查找vtpVlanIfIndex(OID: 1.3.6.1.4.1.9.9.46.1.3.1.1.18)

将上面的  Oid rootOid = new Oid("1.3.6.1.2.1.2.2.1.2"); // ifDescr中换成

      Oid rootOid = new Oid("1.3.6.1.4.1.9.9.46"); // vlain id 好像是这个,需要一个个试一下,待修改,这个OID不在mib2中,用mib浏览器是找不到的,而在mib2上面一层,SMI树中

  使用此OID,得到的结果是  “端口”--》“vlan id”,eg:2 --》123

 

支持(0) 反对(0) 心语2012 | 园豆:216 (菜鸟二级) | 2013-12-07 19:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册