| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1854 人关注过本帖
标题:SNMP编程
只看楼主 加入收藏
shulingyi
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2016-8-24
收藏
 问题点数:0 回复次数:0 
SNMP编程
各位高手们

求助,我想用snmpsharpnet进行一些相关的操作,目的是想通过获取某一台交换机下所有终端(机顶盒)的mac地址,怎么搞?
我找了一些例子,但是调不通
 public static void Main(string[] argv)
    {
        Dictionary<string, string> dic = new Dictionary<string, string>();
        // SNMP community name   
        OctetString community = new OctetString("99");
        // 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("172.24.250.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"); // 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)
                {
                    // 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))
                        {
                            dic.Add(v.Oid.ToString(), v.Value.ToString());
                        }
                        else
                        {
                            // we have reached the end of the requested   
                            // MIB tree. Set lastOid to null and exit loop   
                            lastOid = null;
                        }
                    }
                }
            }
        }
        target.Close();
      

    }
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: public 交换机 机顶盒 
2016-08-24 15:10
快速回复:SNMP编程
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036256 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved