一个C#编写的snmp程序,可使网络管理人员进行参考、开发-A C# Prepared snmp procedures, will enable network managers to carry o

本文介绍了一个使用自定义SNMP库进行系统信息查询的示例程序。该程序通过加载MIB文件,演示了如何利用SNMP协议获取本地主机的系统信息,并展示了get、walk和set操作的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections;
using SNMPDll;


namespace TestSNMPDll
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{

			//LOAD all the MIB found into the directory c:/windows/System32/****************
			Mib myMib = new Mib();
			Console.WriteLine("**************Loading MIB's files**************");
			myMib.loadDirectoryMib(Environment.GetFolderPath(Environment.SpecialFolder.System));
			//****************************************************************************


			//**************DISPLAY THE loading MIB************************
			//myMib.walk(@"c:/aaaaaaa.txt");
			//*************************************************************


			//*************INIT SNMP AGENT*********************
			SNMPAgent myAgent = new SNMPAgent("127.0.0.1","public","public");
			 //if you do that please active the snmp capabilities into your compture : "Add/remove windows component" - "Management and monitoring tools"

			//************INIT SNMP OBJECT*********************
			SNMPObject myRequest = new SNMPObject("1.3.6.1.2.1.1.5.0",myMib);
			Console.WriteLine();
			Console.WriteLine("***************Make the request***************");
			Console.WriteLine("I am looking for the value : " + myRequest.getFullName());
			Console.WriteLine("My value is : " + myRequest.getSimpleValue(myAgent));
			Console.WriteLine("The type of my value is : " + myRequest.getType());
			Console.WriteLine("The description of my value is : " + myRequest.getDescription());

			//**********SOME OTHER TEST**********************
			testWalk();
			testMultiGet();
			testSet();
			Console.ReadLine();
		}


		static private void testMultiGet()
		{
			Console.WriteLine("test multi get");
			Console.WriteLine("--------------");

			//Add many request you want
			SNMPObject[] s = new SNMPObject[2];
			s[0] = new SNMPObject("1.3.6.1.2.1.1.5.0");
			s[1] = new SNMPObject("1.3.6.1.2.1.2.2.1.16.1");

			SNMPAgent a = new SNMPAgent("127.0.0.1","public","public");
			Hashtable ht = a.getValues(s);
			int i = 1;
			while (true)
			{
				if (!ht.Contains(i)) break;
				Console.WriteLine("oid : " + s[i-1].getOID());
				Console.WriteLine("value : " + (string)((Hashtable)ht[i])["value"]);
				Console.WriteLine("type : " + (string)((Hashtable)ht[i])["type"]);
				i++;
			}

			Console.WriteLine("");
			
		}

	

		static private void testWalk()
		{
			Console.WriteLine("test walk");
			Console.WriteLine("---------");

			SNMPObject s = new SNMPObject("1.3.6.1.2.1.2.2.1");
			//SNMPObject s = new SNMPObject("1");
			SNMPAgent a = new SNMPAgent("127.0.0.1","public","public");
			a.walk(s);
			Console.WriteLine("");
		}

		static private void testSet()
		{

			//This procedure will cause a error on your computer, because you cannot change the name of your computer by an SNMP request
			//But of course this will work on a CISCO switch for example
			SNMPAgent myAgent = new SNMPAgent("127.0.0.1","public","public");
			Console.WriteLine("test Set");
			Console.WriteLine("--------");

			Console.WriteLine("Read the value before the modification");
			SNMPObject s = new SNMPObject("1.3.6.1.2.1.1.5.0");
			Hashtable ht = s.getValue(myAgent);

			Console.WriteLine("oid : " + s.getOID());
			Console.WriteLine("value : " + (string)ht["value"]);
			Console.WriteLine("type : " + (string)ht["type"]);

			Console.WriteLine("");

			Console.WriteLine("Set the value");

			s = new SNMPObject("1.3.6.1.2.1.1.5.0");
			myAgent.setValue(s,SNMPDll.SNMPOIDType.OctetString,"DEV-SOFT-ONE");

			Console.WriteLine("");

			Console.WriteLine("Read the value after the modification");

			s = new SNMPObject("1.3.6.1.2.1.1.5.0");
			ht = s.getValue(myAgent);

			Console.WriteLine("oid : " + s.getOID());
			Console.WriteLine("value : " + (string)ht["value"]);
			Console.WriteLine("type : " + (string)ht["type"]);
			




		}
	}
}
 ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值