用C#实现的简易mib browser

本文介绍了一个使用C#实现的简单SNMP MIB浏览器,它能够发送SNMP请求并解析设备的sysName、sysLocation、sysContact和sysUptime信息。代码示例展示了如何构建和发送SNMP请求,以及如何从响应中提取MIB数据。
摘要由CSDN通过智能技术生成

1.source code 

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;


public class SimpleSNMP
{
   public static void Main(string[] argv)
   {
      int commlength, miblength, datatype, datalength, datastart;
      int uptime = 0;
      string output;
      SNMP conn = new SNMP();
      byte[] response = new byte[1024];

      Console.WriteLine("Device SNMP information:");

      // Send sysName SNMP request
      response = conn.get("get", argv[0], argv[1]"1.3.6.1.2.1.1.5.0");
      if (response[0== 0xff)
      {
         Console.WriteLine("No response from {0}", argv[0]);
         return;
      }

      // If response, get the community name and MIB lengths
      commlength = Convert.ToInt16(response[6]);
      miblength = Convert.ToInt16(response[23 + commlength]);

      // Extract the MIB data from the SNMP response
      datatype = Convert.ToInt16(response[24 + commlength + miblength]);
      datalength = Convert.ToInt16(response[25 + commlength + miblength]);
      datastart = 26 + commlength + miblength;
      output = Encoding.ASCII.GetString(response, datastart, datalength);
      Console.WriteLine("  sysName - Datatype: {0}, Value: {1}",
              datatype, output);

      // Send a sysLocation SNMP request
      response = conn.get("get", argv[0], argv[1]"1.3.6.1.2.1.1.6.0");
      if (response[0== 0xff)
      {
         Console.WriteLine("No response from {0}", argv[0]);
         return;
      }

      // If response, get the community name and MIB lengths
      commlength = Convert.ToInt16(response[6]);
      miblength = Convert.ToInt16(response[23 + commlength]);

      // Extract the MIB data from the SNMP response
      datatype = Convert.ToInt16(response[24 + commlength + miblength]);
      datalength = Convert.ToInt16(response[25 + commlength + miblength]);

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值