.Net Core 基于 SnmpSharpNet 开发

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/swjian1997/article/details/95695118

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();
        }
    }
}

输出结果:

 

 

 

 

 
 
展开阅读全文

snmpsharpnet类库开发问题

05-10

近日在开发一个项目,也不算很复杂,就是利用snmp协议来获取服务器的硬盘空间,然后进行实时监控,在网上找了一下,发现一个比较好用的库snmpsharpnet-7-5,写的代码如下,但是出现了一个问题,首先调用的Ip地址172.29.14.98没有问题,但程序运行到下一个IP172.29.13.2地址就出现异常,显示连接超数,但是互换一下IP地址还是前面的iP可以,后面的Ip不行,只调用一次函数两个IP地址都是可以的,就是有先后顺序就不行,我也不知道是什么原因,使用udp协议进行通信,应该不涉及端口的释放,而且已经使用了close函数进行关闭连接,有没有哪位高人使用过这个库,可以指导一下,不胜感激!!!!!rnusing System;rnusing System.Collections.Generic;rnusing System.Linq;rnusing System.Text;rnusing SnmpSharpNet;rnusing System.Net;rnusing System.Web;rnusing System.Data;rnusing System.Data.SqlClient;rnusing System.Threading;rnrnrnrnnamespace snmprnrn class Programrn rn static void Main(string[] args)rn rn double[] diskstorage1,diskstorage2;rn rn diskstorage1 = snmpget("172.29.14.98", "public", 1);rn rn diskstorage2 = snmpget("172.29.13.2", "pubilc",2);rn foreach (double dou in diskstorage2)rn Console.WriteLine(dou.ToString());rn rn rn rn rn rnstatic double[] snmpget(string ipaddress,string comname,int i)rn rn rn double cvolumn = 0, dvolumn = 0, cvolunmn1 = 0, dvolumn1 = 0, cvolumn2 = 0, dvolumn2 = 0;rn double[] backup = new double[6];rn // SNMP community namern OctetString community = new OctetString(comname);rnrn // Define agent parameters classrn AgentParameters param = new AgentParameters(community);rn // Set SNMP version to 1 (or 2)rn param.Version = (int)SnmpVersion.Ver1;rn // Construct the agent address objectrn // IpAddress class is easy to use here becausern // it will try to resolve constructor parameter if it doesn'trn // parse to an IP addressrn IpAddress agent = new IpAddress(ipaddress);rnrn // Construct targetrn UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 2);rnrn // Pdu class used for all requestsrn Pdu pdu = new Pdu(PduType.Get);rn //区分两台服务器的硬盘mib代码rn if (i == 2)rn rn pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.5.2"); //硬盘C盘簇数rn pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.5.3"); //硬盘D盘簇数rn pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.6.2");//硬盘C盘已用簇数rn pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.6.3");//硬盘D盘已用簇数rn pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.4.2");//硬盘C盘分配单元rn pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.4.3");//硬盘D盘分配单元rn rn else if(i==1)rn rn pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.5.1"); //硬盘C盘簇数rn pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.5.2"); //硬盘D盘簇数rn pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.6.1");//硬盘C盘已用簇数rn pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.6.2");//硬盘D盘已用簇数rn pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.4.1");//硬盘C盘分配单元rn pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.4.2");//硬盘D盘分配单元rn rnrn SnmpV1Packet result = new SnmpV1Packet();rn rn // Make SNMP requestrn result = (SnmpV1Packet)target.Request(pdu, param);rn rn rn rn rn // If result is null then agent didn't reply or we couldn't parse the reply.rn if (result != null)rn rn // ErrorStatus other then 0 is an error returned by rn // the Agent - see SnmpConstants for error definitionsrn if (result.Pdu.ErrorStatus != 0)rn rn // agent reported an error with the requestrn Console.WriteLine("Error in SNMP reply. Error 0 index 1",rn result.Pdu.ErrorStatus,rn result.Pdu.ErrorIndex);rn rn elsern rn // Reply variables are returned in the same order as they were addedrn // to the VbListrnrn int a = int.Parse(result.Pdu.VbList[0].Value.ToString());rn int b = int.Parse(result.Pdu.VbList[1].Value.ToString());rn int c = int.Parse(result.Pdu.VbList[2].Value.ToString());rn int d = int.Parse(result.Pdu.VbList[3].Value.ToString());rn int num1 = int.Parse(result.Pdu.VbList[4].Value.ToString());rn int num2 = int.Parse(result.Pdu.VbList[5].Value.ToString());rn cvolumn =(double) a * num1/1024/1024/1024;rn dvolumn = (double)b * num2 / 1024 / 1024 / 1024;rn cvolunmn1 = (double)c * num1 / 1024 / 1024 / 1024;rn dvolumn1 = (double)d * num2 / 1024 / 1024 / 1024;rn cvolumn2 = cvolumn - cvolunmn1;rn dvolumn2 = dvolumn - dvolumn1;rn backup[0] = cvolumn;rn backup[1] = dvolumn;rn backup[2] = cvolunmn1;rn backup[3] = dvolumn1;rn backup[4] = cvolumn2;rn backup[5] = dvolumn2;rn Console.WriteLine("c盘空间0", cvolumn.ToString());rn Console.WriteLine("d盘空间0", dvolumn.ToString());rn Console.WriteLine("c盘已用空间0", cvolunmn1.ToString());rn Console.WriteLine("d盘已用空间0", dvolumn1.ToString());rn Console.WriteLine("c盘剩余空间0", cvolumn2.ToString());rn Console.WriteLine("d盘剩余空间0", dvolumn2.ToString());rn rn rn rn elsern rn Console.WriteLine("No response received from SNMP agent.");rn rn target.Close();rn rn rn return backup;rn rn rn rn rn rnrnrnrn 论坛

没有更多推荐了,返回首页