惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
WordPress大学
WordPress大学
博客园 - Franky
V
V2EX
爱范儿
爱范儿
J
Java Code Geeks
小众软件
小众软件
Last Week in AI
Last Week in AI
The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
酷 壳 – CoolShell
酷 壳 – CoolShell
The Register - Security
The Register - Security
GbyAI
GbyAI
Vercel News
Vercel News
Y
Y Combinator Blog
腾讯CDC
F
Fortinet All Blogs
I
InfoQ
N
Netflix TechBlog - Medium
B
Blog RSS Feed
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
量子位
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
H
Help Net Security
云风的 BLOG
云风的 BLOG
A
About on SuperTechFans
Scott Helme
Scott Helme
T
Tor Project blog
U
Unit 42
Google Online Security Blog
Google Online Security Blog
PCI Perspectives
PCI Perspectives
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
NISL@THU
NISL@THU
D
Darknet – Hacking Tools, Hacker News & Cyber Security
aimingoo的专栏
aimingoo的专栏
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
Security Archives - TechRepublic
Security Archives - TechRepublic

博客园 - 李华星

[原创]解决某物流企业二维码打印问题 The Workflow Management Coalition Specification 招聘.net软件开发工程师 AgilePoint BPMS软件评估分析 谈谈对BPM的理解 SharePoint WebPart开发步骤 原创对联大观园 SmartPublisher设计之旅 — 软件设计无需后悔莫及 SNMP基础简介 SmartPublisher设计之旅 — 软件开发之前先做个梦 SmartPublisher设计之旅 — 让理想在仇恨中茁壮成长 - 李华星 没事儿就来说些荒唐事儿 SmartPublisher设计之旅 — 优秀的设计才是让软件永葆青春的秘诀 - 李华星 SmartPublisher设计之旅 — 打好基础是实现理想的首要任务 - 李华星 分享一套.Net开发环境下的界面框架组件 SmartPublisher设计之旅 — 让梦想成为理想 设计模式系列漫谈之十三 - 访问者模式 SmartPublisher设计之旅 设计模式系列漫谈之十二 - 职责链模式
.NET环境下的SNMP编程 - 李华星 - 博客园
李华星 · 2008-01-20 · via 博客园 - 李华星

下面的例子是通过SNMP获取指定IP的机器名。

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;
            packet[pos
++= 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;
        }
    }
}

运行结果如下图: