欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#简单实现SNMP的方法

程序员文章站 2023-12-10 19:43:46
本文实例讲述了c#简单实现snmp的方法。分享给大家供大家参考。具体如下: /** c# network programming by richard bl...

本文实例讲述了c#简单实现snmp的方法。分享给大家供大家参考。具体如下:

/**
c# network programming 
by richard blum
publisher: sybex 
isbn: 0782141765
*/
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]);
   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);
  }
}
class snmp
{
  public snmp()
  {
  }
  public byte[] get(string request, string host, string community, string mibstring)
  {
   byte[] packet = new byte[1024];
   byte[] mib = new byte[1024];
   int snmplen;
   int comlen = community.length;
   string[] mibvals = mibstring.split('.');
   int miblen = mibvals.length;
   int cnt = 0, temp, i;
   int orgmiblen = miblen;
   int pos = 0;
   // convert the string mib into a byte array of integer values
   // unfortunately, values over 128 require multiple bytes
   // which also increases the mib length
   for (i = 0; i < orgmiblen; i++)
   {
     temp = convert.toint16(mibvals[i]);
     if (temp > 127)
     {
      mib[cnt] = convert.tobyte(128 + (temp / 128));
      mib[cnt + 1] = convert.tobyte(temp - ((temp / 128) * 128));
      cnt += 2;
      miblen++;
     } else
     {
      mib[cnt] = convert.tobyte(temp);
      cnt++;
     }
   }
   snmplen = 29 + comlen + miblen - 1; //length of entire snmp packet
   //the snmp sequence start
   packet[pos++] = 0x30; //sequence start
   packet[pos++] = convert.tobyte(snmplen - 2); //sequence size
   //snmp version
   packet[pos++] = 0x02; //integer type
   packet[pos++] = 0x01; //length
   packet[pos++] = 0x00; //snmp version 1
   //community name
   packet[pos++] = 0x04; // string type
   packet[pos++] = convert.tobyte(comlen); //length
   //convert community name to byte array
   byte[] data = encoding.ascii.getbytes(community);
   for (i = 0; i < data.length; i++)
   {
     packet[pos++] = data[i];
   }
   //add getrequest or getnextrequest value
   if (request == "get")
     packet[pos++] = 0xa0;
   else
     packet[pos++] = 0xa1;
   packet[pos++] = convert.tobyte(20 + miblen - 1); //size of total mib
   //request id
   packet[pos++] = 0x02; //integer type
   packet[pos++] = 0x04; //length
   packet[pos++] = 0x00; //snmp request id
   packet[pos++] = 0x00;
   packet[pos++] = 0x00;
   packet[pos++] = 0x01;
   //error status
   packet[pos++] = 0x02; //integer type
   packet[pos++] = 0x01; //length
   packet[pos++] = 0x00; //snmp error status
   //error index
   packet[pos++] = 0x02; //integer type
   packet[pos++] = 0x01; //length
   packet[pos++] = 0x00; //snmp error index
   //start of variable bindings
   packet[pos++] = 0x30; //start of variable bindings sequence
   packet[pos++] = convert.tobyte(6 + miblen - 1); // size of variable binding
   packet[pos++] = 0x30; //start of first variable bindings sequence
   packet[pos++] = convert.tobyte(6 + miblen - 1 - 2); // size
   packet[pos++] = 0x06; //object type
   packet[pos++] = convert.tobyte(miblen - 1); //length
   //start of mib
   packet[pos++] = 0x2b;
   //place mib array in packet
   for(i = 2; i < miblen; i++)
     packet[pos++] = convert.tobyte(mib[i]);
   packet[pos++] = 0x05; //null object value
   packet[pos++] = 0x00; //null
   //send packet to destination
   socket sock = new socket(addressfamily.internetwork, sockettype.dgram,
            protocoltype.udp);
   sock.setsocketoption(socketoptionlevel.socket,
           socketoptionname.receivetimeout, 5000);
   iphostentry ihe = dns.resolve(host);
   ipendpoint iep = new ipendpoint(ihe.addresslist[0], 161);
   endpoint ep = (endpoint)iep;
   sock.sendto(packet, snmplen, socketflags.none, iep);
   //receive response from packet
   try
   {
     int recv = sock.receivefrom(packet, ref ep);
   } catch (socketexception)
   {
     packet[0] = 0xff;
   }
   return packet;
  }
  public string getnextmib(byte[] mibin)
  {
   string output = "1.3";
   int commlength = mibin[6];
   int mibstart = 6 + commlength + 17; //find the start of the mib section
   //the mib length is the length defined in the snmp packet
   // minus 1 to remove the ending .0, which is not used
   int miblength = mibin[mibstart] - 1;
   mibstart += 2; //skip over the length and 0x2b values
   int mibvalue;
   for(int i = mibstart; i < mibstart + miblength; i++)
   {
     mibvalue = convert.toint16(mibin[i]);
     if (mibvalue > 128)
     {
      mibvalue = (mibvalue/128)*128 + convert.toint16(mibin[i+1]);
      //error here, it should be mibvalue = (mibvalue-128)*128 + convert.toint16(mibin[i+1]);
      //for mib values greater than 128, the math is not adding up correctly  
      i++;
     }
     output += "." + mibvalue;
   }
   return output;
  }
}

希望本文所述对大家的c#程序设计有所帮助。