• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

用AdventNet SNMP API获取SNMP表信息(C#)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

用AdventNet SNMP API获取SNMP表信息(C#)

用AdventNet SNMP API获取SNMP表信息(C#)

 

using System;

using adventnet.snmp.snmp2;

public class snmpget

{

    [STAThread]

    public static void Main(System.String[] args)

    {

 

        // getting the hostname and the OID from the command line

 

        // Start SNMP API

        SnmpAPI api = new SnmpAPI();

        //api.Debug = true;

 

        // Open session

        SnmpSession session = new SnmpSession(api);

        //Build GET Request PDU

        SnmpPDU pdu = new SnmpPDU();

        pdu.Community = "public";

        pdu.WriteCommunity = "jinyun888";

        System.String remoteHost = "211.101.116.112";

        UDPProtocolOptions option = new UDPProtocolOptions(remoteHost);

        pdu.ProtocolOptions = option;

        pdu.Timeout = 10000;

        pdu.Retries = 3;

        pdu.Command = adventnet.snmp.snmp2.SnmpAPI.GETNEXT_REQ_MSG;

        SnmpOID[] oids = new SnmpOID[3];

        oids[0] = new SnmpOID(".1.3.6.1.4.1.429.1.1.2.1.1.1");

        oids[1] = new SnmpOID(".1.3.6.1.4.1.429.1.1.2.1.1.3");

        oids[2] = new SnmpOID(".1.3.6.1.4.1.429.1.1.2.1.1.4");

       

        for (int i = 0; i < 3; i++)

        {

            pdu.AddNull(oids[i]);

        }

        SnmpOID rootoid = new SnmpOID(".1.3.6.1.4.1.429.1.1.2.1.1.1.");

        String root = rootoid.ToString();

        try

        {

            session.Open();

        }

        catch (SnmpException e)

        {

            System.Console.Error.WriteLine("Error opening socket: " + e);

        }

        // add OIDs

        while (true)

        // until received OID isn't in sub-tree

        {

            try

            {

                // Send PDU and receive response PDU

                pdu = session.SyncSend(pdu);

            }

            catch (SnmpException e)

            {

                System.Console.Error.WriteLine("Sending PDU" + e.Message);

                System.Environment.Exit(1);

            }

            if (pdu == null)

            {

                System.Console.Out.WriteLine("Request timed out to: " );

                System.Environment.Exit(1);

            }

            //check for out index

            if (!(pdu.GetObjectID(0).ToString().StartsWith(root)))

            {

                break;

            }

            int version = pdu.Version;

            if (version == SnmpAPI.SNMP_VERSION_1)

            {

                // check for error

                if (pdu.Errstat != 0)

                {

                    System.Console.Out.WriteLine("Error Indication in response: " + SnmpException.ExceptionString((sbyte)pdu.Errstat) + "\nErrindex: " + pdu.Errindex);

                    System.Environment.Exit(1);

                }

                // print response pdu variable-bindings                   

                System.Console.Out.WriteLine(pdu.PrintVarBinds());

            }

            else if (version == SnmpAPI.SNMP_VERSION_2C)

            {

                System.Collections.IEnumerator e = pdu.VariableBindings.GetEnumerator();

                while (e.MoveNext())

                {

                    int error = 0;

                    SnmpVarBind varbind = (SnmpVarBind)e.Current;

                    // check for error

                    if ((error = varbind.Errindex) != 0)

                    {

                        System.Console.Out.WriteLine("Error Indication in response: " + SnmpException.ExceptionString((sbyte)error));

                        System.Environment.Exit(1);

                    }

                    // print response pdu variable-bindings

                    System.Console.Out.WriteLine(pdu.PrintVarBinds());

                }

            }

            else

            {

                System.Console.Out.WriteLine("Invalid Version Number");

            }

            // set GETNEXT_REQ_MSG to do walk

            // Don't forget to set request id to 0 otherwise next request will fail

            pdu.Reqid = 0;

            pdu.Command = adventnet.snmp.snmp2.SnmpAPI.GETNEXT_REQ_MSG;

        } // end of while true

 

        // close session

        session.Close();

        //close the api thread

        api.Close();

    }

}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
【C#】如何在listview中插入图片发布时间:2022-07-13
下一篇:
C#二进制序列化发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap