C#简单实现SNMP的方法
本文实例讲述了C#简单实现SNMP的方法。分享给大家供大家参考。具体如下:
/**
C#NetworkProgramming
byRichardBlum
Publisher:Sybex
ISBN:0782141765
*/
usingSystem;
usingSystem.Text;
usingSystem.Net;
usingSystem.Net.Sockets;
publicclassSimpleSNMP
{
publicstaticvoidMain(string[]argv)
{
intcommlength,miblength,datatype,datalength,datastart;
intuptime=0;
stringoutput;
SNMPconn=newSNMP();
byte[]response=newbyte[1024];
Console.WriteLine("DeviceSNMPinformation:");
//SendsysNameSNMPrequest
response=conn.get("get",argv[0],argv[1],"1.3.6.1.2.1.1.5.0");
if(response[0]==0xff)
{
Console.WriteLine("Noresponsefrom{0}",argv[0]);
return;
}
//Ifresponse,getthecommunitynameandMIBlengths
commlength=Convert.ToInt16(response[6]);
miblength=Convert.ToInt16(response[23+commlength]);
//ExtracttheMIBdatafromtheSNMPresponse
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);
//SendasysLocationSNMPrequest
response=conn.get("get",argv[0],argv[1],"1.3.6.1.2.1.1.6.0");
if(response[0]==0xff)
{
Console.WriteLine("Noresponsefrom{0}",argv[0]);
return;
}
//Ifresponse,getthecommunitynameandMIBlengths
commlength=Convert.ToInt16(response[6]);
miblength=Convert.ToInt16(response[23+commlength]);
//ExtracttheMIBdatafromtheSNMPresponse
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);
//SendasysContactSNMPrequest
response=conn.get("get",argv[0],argv[1],"1.3.6.1.2.1.1.4.0");
if(response[0]==0xff)
{
Console.WriteLine("Noresponsefrom{0}",argv[0]);
return;
}
//GetthecommunityandMIBlengths
commlength=Convert.ToInt16(response[6]);
miblength=Convert.ToInt16(response[23+commlength]);
//ExtracttheMIBdatafromtheSNMPresponse
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);
//SendaSysUptimeSNMPrequest
response=conn.get("get",argv[0],argv[1],"1.3.6.1.2.1.1.3.0");
if(response[0]==0xff)
{
Console.WriteLine("Noresponsefrom{0}",argv[0]);
return;
}
//GetthecommunityandMIBlengthsoftheresponse
commlength=Convert.ToInt16(response[6]);
miblength=Convert.ToInt16(response[23+commlength]);
//ExtracttheMIBdatafromtheSNMpresponse
datatype=Convert.ToInt16(response[24+commlength+miblength]);
datalength=Convert.ToInt16(response[25+commlength+miblength]);
datastart=26+commlength+miblength;
//ThesysUptimevaluemaybyamulti-byteinteger
//Eachbytereadmustbeshiftedtothehigherbyteorder
while(datalength>0)
{
uptime=(uptime<<8)+response[datastart++];
datalength--;
}
Console.WriteLine("sysUptime-Datatype:{0},Value:{1}",
datatype,uptime);
}
}
classSNMP
{
publicSNMP()
{
}
publicbyte[]get(stringrequest,stringhost,stringcommunity,stringmibstring)
{
byte[]packet=newbyte[1024];
byte[]mib=newbyte[1024];
intsnmplen;
intcomlen=community.Length;
string[]mibvals=mibstring.Split('.');
intmiblen=mibvals.Length;
intcnt=0,temp,i;
intorgmiblen=miblen;
intpos=0;
//ConvertthestringMIBintoabytearrayofintegervalues
//Unfortunately,valuesover128requiremultiplebytes
//whichalsoincreasestheMIBlength
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;//LengthofentireSNMPpacket
//TheSNMPsequencestart
packet[pos++]=0x30;//Sequencestart
packet[pos++]=Convert.ToByte(snmplen-2);//sequencesize
//SNMPversion
packet[pos++]=0x02;//Integertype
packet[pos++]=0x01;//length
packet[pos++]=0x00;//SNMPversion1
//Communityname
packet[pos++]=0x04;//Stringtype
packet[pos++]=Convert.ToByte(comlen);//length
//Convertcommunitynametobytearray
byte[]data=Encoding.ASCII.GetBytes(community);
for(i=0;i<data.Length;i++)
{
packet[pos++]=data[i];
}
//AddGetRequestorGetNextRequestvalue
if(request=="get")
packet[pos++]=0xA0;
else
packet[pos++]=0xA1;
packet[pos++]=Convert.ToByte(20+miblen-1);//SizeoftotalMIB
//RequestID
packet[pos++]=0x02;//Integertype
packet[pos++]=0x04;//length
packet[pos++]=0x00;//SNMPrequestID
packet[pos++]=0x00;
packet[pos++]=0x00;
packet[pos++]=0x01;
//Errorstatus
packet[pos++]=0x02;//Integertype
packet[pos++]=0x01;//length
packet[pos++]=0x00;//SNMPerrorstatus
//Errorindex
packet[pos++]=0x02;//Integertype
packet[pos++]=0x01;//length
packet[pos++]=0x00;//SNMPerrorindex
//Startofvariablebindings
packet[pos++]=0x30;//Startofvariablebindingssequence
packet[pos++]=Convert.ToByte(6+miblen-1);//Sizeofvariablebinding
packet[pos++]=0x30;//Startoffirstvariablebindingssequence
packet[pos++]=Convert.ToByte(6+miblen-1-2);//size
packet[pos++]=0x06;//Objecttype
packet[pos++]=Convert.ToByte(miblen-1);//length
//StartofMIB
packet[pos++]=0x2b;
//PlaceMIBarrayinpacket
for(i=2;i<miblen;i++)
packet[pos++]=Convert.ToByte(mib[i]);
packet[pos++]=0x05;//Nullobjectvalue
packet[pos++]=0x00;//Null
//Sendpackettodestination
Socketsock=newSocket(AddressFamily.InterNetwork,SocketType.Dgram,
ProtocolType.Udp);
sock.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout,5000);
IPHostEntryihe=Dns.Resolve(host);
IPEndPointiep=newIPEndPoint(ihe.AddressList[0],161);
EndPointep=(EndPoint)iep;
sock.SendTo(packet,snmplen,SocketFlags.None,iep);
//Receiveresponsefrompacket
try
{
intrecv=sock.ReceiveFrom(packet,refep);
}catch(SocketException)
{
packet[0]=0xff;
}
returnpacket;
}
publicstringgetnextMIB(byte[]mibin)
{
stringoutput="1.3";
intcommlength=mibin[6];
intmibstart=6+commlength+17;//findthestartofthemibsection
//TheMIBlengthisthelengthdefinedintheSNMPpacket
//minus1toremovetheending.0,whichisnotused
intmiblength=mibin[mibstart]-1;
mibstart+=2;//skipoverthelengthand0x2bvalues
intmibvalue;
for(inti=mibstart;i<mibstart+miblength;i++)
{
mibvalue=Convert.ToInt16(mibin[i]);
if(mibvalue>128)
{
mibvalue=(mibvalue/128)*128+Convert.ToInt16(mibin[i+1]);
//ERRORhere,itshouldbemibvalue=(mibvalue-128)*128+Convert.ToInt16(mibin[i+1]);
//formibvaluesgreaterthan128,themathisnotaddingupcorrectly
i++;
}
output+="."+mibvalue;
}
returnoutput;
}
}
希望本文所述对大家的C#程序设计有所帮助。