zt .NET環境下的SNMP程式設計

zhengnx發表於2011-10-31

using System;

using System.Text;

using System.Net;

using System.Net.Sockets;

namespace ConsoleSNMP

{ public class Program

{ static void Main()

{ string host = "192.168.1.1";

string communityName = "public";

SNMP conn = new SNMP();

byte[] response = new byte[1024];

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

response = conn.get("GetRequest", host, communityName, "1.3.6.1.2.1.1.5.0");

if (response[0] == 0xff)

{ Console.WriteLine("No response from {0}", host);

return; }

int commlength = Convert.ToInt16(response[6]);

int miblength = Convert.ToInt16(response[23 + commlength]);

int datatype = Convert.ToInt16(response[24 + commlength + miblength]);

int datalength = Convert.ToInt16(response[25 + commlength + miblength]);

int datastart = 26 + commlength + miblength;

string output = Encoding.ASCII.GetString(response, datastart, datalength); Console.WriteLine(" 機器名: - Datatype: {0}, Value: {1}", datatype, output); Console.ReadLine();

} }

class 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=0, i=0;

int orgmiblen = miblen;

int pos = 0; //將mib字串格式轉換為整數格式,如果值大於128需要多個位元組空間儲存,同時增加mib庫的長度

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; //snmp包序列開始

packet[pos++] = 0x30; packet[pos++] = Convert.ToByte(snmplen - 2); //snmp版本號

packet[pos++] = 0x02;//整數型別

packet[pos++] = 0x01;//長度

packet[pos++] = 0x01;//snmp 版本1 //團體名稱

packet[pos++] = 0x04;//字串型別

packet[pos++] = Convert.ToByte(comlen);//團體名稱長度

byte[] data = Encoding.ASCII.GetBytes(community);

for (i = 0; i < data.Length; i++) { packet[pos++] = data[i];//轉化團體名稱到包中 } //新增GetRequest 或是GetNextRequest 值

if (request == "GetRequest") { packet[pos++] = 0xA0; } else { packet[pos++] = 0xA1; } packet[pos++] = Convert.ToByte(20 + miblen - 1); //Request ID packet[pos++] = 0x02;//整數型別

packet[pos++] = 0x04;//長度

packet[pos++] = 0x00;//SNMP request ID

packet[pos++] = 0x00; ++] = 0x00;

packet[pos++] = 0x01; //錯誤狀態

packet[pos++] = 0x02;//整數型別

packet[pos++] = 0x01;//長度

packet[pos++] = 0x00;//SNMP錯誤型別 //錯誤索引

packet[pos++] = 0x02;//整數型別

packet[pos++] = 0x01;//長度

packet[pos++] = 0x00;//SNMP錯誤索引

packet[pos++] = 0x30;//不同物件序列開始標誌

packet[pos++] = Convert.ToByte(6 + miblen - 1);//不同物件的長度

packet[pos++] = 0x30;//物件序列的開始

packet[pos++] = Convert.ToByte(6 + miblen - 1 - 2);//尺寸

packet[pos++] = 0x06;//物件型別

packet[pos++] = Convert.ToByte(miblen - 1);//長度 //MIB資訊開始位置

packet[pos++] = 0x2b; //把MIB數字放入包中

for (i = 2; i < miblen; i++)

{ packet[pos++] = Convert.ToByte(mib[i]); }

packet[pos++] = 0x05;//空物件值

packet[pos++] = 0x00;////透過Socket把包送到指定機器

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);

try { int recv = sock.ReceiveFrom(packet, ref ep);//接受返回資料 } catch (SocketException)

{ packet[0] = 0xff; }

return packet;

} } }

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7868752/viewspace-1056070/,如需轉載,請註明出處,否則將追究法律責任。

相關文章