.Net Core 基於 SnmpSharpNet 開發


SNMP簡介(百度百科):

  SNMP 是專門設計用於在 IP 網絡管理網絡節點(服務器、工作站、路由器、交換機及HUBS等)的一種標准協議,它是一種應用層協議。 SNMP 使網絡管理員能夠管理網絡效能,發現並解決網絡問題以及規划網絡增長。通過 SNMP 接收隨機消息(及事件報告)網絡管理系統獲知網絡出現問題。

C#中簡單實現

一、首先在NuGet中導入  SnmpSharpNet-core,當然如果是.net 可以導入SnmpSharpNet。

二、編寫代碼,本案例是查詢打印機狀態、打印紙張數量信息。

using SnmpSharpNet;
using System;
using System.Net;

namespace SnmpSharpNetDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            OctetString community = new OctetString("public");

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

            // Construct target
            UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);

            // Pdu class used for all requests
            Pdu pdu = new Pdu(PduType.Get);
            pdu.VbList.Add("1.3.6.1.2.1.43.10.2.1.4.1.1");      //Oid 值
            pdu.VbList.Add("1.3.6.1.2.1.25.3.5.1.1.1");         //Oid 值
            
            SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);

            for( int i=0;i<result.Pdu.VbList.Count;i++)
            {
                string strTemp = string.Format("Oid({0}) ({1}): {2} \r\n",
                        result.Pdu.VbList[i].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[i].Value.Type),
                        result.Pdu.VbList[i].Value.ToString());

                Console.WriteLine(strTemp);
            } 
            Console.ReadLine();
        }
    }
}

輸出結果:

 

 

 

 

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM