一个SNMP类!
1using System;
2using System.Net;
3using System.Net.Sockets;
4using System.Text;
5class SNMP
6{
7 public SNMP()
8 {
9 }
10 public byte[] get(string request, string host, string community, string_ mibstring)
11 {
12 byte[] packet = new byte[1024];
13 byte[] mib = new byte[1024];
14 int snmplen;
15 int comlen = community.Length;
16 string[] mibvals = mibstring.Split('.');
17 int miblen = mibvals.Length;
18 int cnt = 0, temp, i;
19 int orgmiblen = miblen;
20 int pos = 0;
21 // Convert the string MIB into a byte array of integer values
22 // Unfortunately, values over 128 require multiple bytes
23 // which also increases the MIB length
24 for (i = 0; i < orgmiblen; i++)
25 {
26 temp = Convert.ToInt16(mibvals[i]);
27 if (temp > 127)
28 {
29 mib[cnt] = Convert.ToByte(128 + (temp / 128));
30 mib[cnt + 1] = Convert.ToByte(temp - ((temp / 128) * 128));
31 cnt += 2;
32 miblen++;
33 } else
34 {
35 mib[cnt] = Convert.ToByte(temp);
36 cnt++;
37 }
38 }
39 snmplen = 29 + comlen + miblen - 1; //Length of entire SNMP packet
40 //The SNMP sequence start
41 packet[pos++] = 0x30; //Sequence start
42 packet[pos++] = Convert.ToByte(snmplen - 2); //sequence size
43 //SNMP version
44 packet[pos++] = 0x02; //Integer type
45 packet[pos++] = 0x01; //length
46 packet[pos++] = 0x00; //SNMP version 1
47 //Community name
48 packet[pos++] = 0x04; // String type
49 packet[pos++] = Convert.ToByte(comlen); //length
50 //Convert community name to byte array
51 byte[] data = Encoding.ASCII.GetBytes(community);
52 for (i = 0; i < data.Length; i++)
53 {
54 packet[pos++] = data[i];
55 }
56 //Add GetRequest or GetNextRequest value
57 if (request == "get")
58 packet[pos++] = 0xA0;
59 else
60 packet[pos++] = 0xA1;
61 packet[pos++] = Convert.ToByte(20 + miblen - 1); //Size of total MIB
62 //Request ID
63 packet[pos++] = 0x02; //Integer type
64 packet[pos++] = 0x04; //length
65 packet[pos++] = 0x00; //SNMP request ID
66 packet[pos++] = 0x00;
67 packet[pos++] = 0x00;
68 packet[pos++] = 0x01;
69 //Error status
70 packet[pos++] = 0x02; //Integer type
71 packet[pos++] = 0x01; //length
72 packet[pos++] = 0x00; //SNMP error status
73 //Error index
74 packet[pos++] = 0x02; //Integer type
75 packet[pos++] = 0x01; //length
76 packet[pos++] = 0x00; //SNMP error index
77 //Start of variable bindings
78 packet[pos++] = 0x30; //Start of variable bindings sequence
79 packet[pos++] = Convert.ToByte(6 + miblen - 1); // Size of variable binding
80 packet[pos++] = 0x30; //Start of first variable bindings sequence
81 packet[pos++] = Convert.ToByte(6 + miblen - 1 - 2); // size
82 packet[pos++] = 0x06; //Object type
83 packet[pos++] = Convert.ToByte(miblen - 1); //length
84 //Start of MIB
85 packet[pos++] = 0x2b;
86 //Place MIB array in packet
87 for(i = 2; i < miblen; i++)
88 packet[pos++] = Convert.ToByte(mib[i]);
89 packet[pos++] = 0x05; //Null object value
90 packet[pos++] = 0x00; //Null
91 //Send packet to destination
92 Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
93 ProtocolType.Udp);
94 sock.SetSocketOption(SocketOptionLevel.Socket,
95 SocketOptionName.ReceiveTimeout, 5000);
96 IPHostEntry ihe = Dns.Resolve(host);
97 IPEndPoint iep = new IPEndPoint(ihe.AddressList[0], 161);
98 EndPoint ep = (EndPoint)iep;
99 sock.SendTo(packet, snmplen, SocketFlags.None, iep);
100 //Receive response from packet
101 try
102 {
103 int recv = sock.ReceiveFrom(packet, ref ep);
104 } catch (SocketException)
105 {
106 packet[0] = 0xff;
107 }
108 return packet;
109 }
110}
111
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。