SNMP'System.IndexOutOfRangeException'错误

时间:2017-09-25 11:20:00

标签: c# exception snmp mib

通过阅读和执行Newtork Programming book中的示例来尝试理解和学习什么是snmp。 执行程序时,我遇到错误(在字符串MIB结构上)(第435页)。我刚刚将SNMP类(第430页)插入到项目中。

问题是:

  

SimpleSNMP.exe中出现未处理的“System.IndexOutOfRangeException”类型异常

代码行

response = conn.get("get", argv[0], argv[1], "1.3.6.1.2.1.1.5.0");

我想通过使用snmp来学习远程设备的状态。非常有用,有用且有效的c#SNMP示例表示赞赏。

所有代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace SimpleSNMP
{
    class Program
    {
        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 and get the response
            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;
            }
            // Get the community name and MIB lengths from the packet
            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;
            }
            // Get the community name and MIB lengths from the response
            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(" sysLocation - Datatype: {0}, Value: {1}",
            datatype, output);
            // Send a sysContact SNMP request
            response = conn.get("get", argv[0], argv[1], "1.3.6.1.2.1.1.4.0");
            if (response[0] == 0xff)
            {
                Console.WriteLine("No response from {0}", argv[0]);
                return;
            }
            // Get the community 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(" sysContact - Datatype: {0}, Value: {1}",
            datatype, output);
            // Send a SysUptime SNMP request
            response = conn.get("get", argv[0], argv[1], "1.3.6.1.2.1.1.3.0");
            if (response[0] == 0xff)
            {
                Console.WriteLine("No response from {0}", argv[0]);
                return;
            }

        // Get the community and MIB lengths of the response
        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;
        // The sysUptime value may by a multi-byte integer
        // Each byte read must be shifted to the higher byte order
        while(datalength > 0)
        {
        uptime = (uptime << 8) + response[datastart++];
        datalength++ ;
        }
            Console.WriteLine(" sysUptime - Datatype: {0}, Value: {1}",
        datatype, uptime);
        }
    }
}

0 个答案:

没有答案