[求助]在C#中如何使用SNMP? ( 积分: 300 )

  • 主题发起人 主题发起人 yu_ting
  • 开始时间 开始时间
Y

yu_ting

Unregistered / Unconfirmed
GUEST, unregistred user!
如题,不能使用WMI,因为访问的设备不一定是windows操作系统。
 
如题,不能使用WMI,因为访问的设备不一定是windows操作系统。
 
snmp是协议,不是什么wmi或者windows啊,看你运行该程序的操作系统平台是什么了,如果是windows的话,winsnmp api提供了你需要的功能,至于c#有没有类似的组件,我就不了解了,因为没有用过c#,不要笑,哈哈!
 
知道有个wsnmp.dll,但不知道在C#中如何导出其中的函数,哪位有wsnmp.dll的文档?
 
给你贴个地址:
http://www.c-sharpcorner.com/Code/2002/Sept/SnmpLib.asp
另外,自己在google上搜索一下,国外有很多高手。
 
cqwty,感谢你的帮助,这个SnmpCode.zip我已经下载过了,源码本身有BUG。取MIB树、MIB列表都没有问题,但取MIB值时程序就死了。
 
难道我要用socket连接udp的161端口,然后通过rfc1157来组装、发送、接收、解析数据包吗?
 
不用吧,你说的mib树是什么?mib列表是什么?对了,顺便问一句,你了解过snmp的工作原理吗?还有你的mib树是什么设备上面的,mib列表是什么设备上面的?mib值又是什么上面的?
 
管理信息数据库(MIB)是由SNMP代理维护的一个信息存储库,是一个具有分层特性的信息的集合,它可以被网络管理系统控制。SNMP通过SNMP代理来控制MIB数据对象。
MIB是整个SNMP协议的核心,在Agent端来说,MIB记录了可管理的对象的属性集合;在管理工作站方,需要轮询时把对应的MIB转换成中间结构传递通过网络传递给Agent,Agent轮询资料后反馈给管理工作站。
 
既然你能够浏览mib树了,为什么会取不了值呢?不是有对应的oid吗?
每个mib库里面的节点都有一个oid值的,在delphi里面,就是通过request操作,
发送oid给agent,然后就可以收到由agent返回来的对应值了。
 
我正在导出wsnmp.dll的函数,封装起来的确很麻烦。cqwty,你说的流程不错,但必须借助于wsnmp.dll中的API函数。
 
我自己开发了一个网络管理系统,不过使用的是现成的控件,当时本来想自己封装的,太累了,偷懒用了别人的控件,哈哈。不是标准程序员风格,不要笑我。
 
当然在别人的基础上继续开发。cqwty,你用的控件是通过什么方式连接SNMP的?直接连接161端口,还是使用了wsnmp.dll文件?
 
161端口是默认的,也就是设备上开的端口,而162端口是本机开的端口。snmp协议是通过udp协议的,所以是不面向连接的。网上有关于winsnmp方面的资料,还有就是现在有很多代码提供了对.net的支持,你可以在google上搜索“网络管理 论坛”这个关键字,其中有一个就是西陆的,这个论坛上不错,有你需要的东西。
 
好的,谢谢!我去看看。
 
国外已经有高手导出wsnmp.dll的API了,有三个文件:Functions.cs,Enumns.cs,structs.cs。
 
Functions.cs
using System;
using System.Runtime.InteropServices;
namespace WinSnmp
{
/// <summary>
/// Snmp API Functions
/// </summary>
public class SnmpAPI
{
// Callback
public delegate SNMPAPI_STATUS SnmpCallback(IntPtr session, IntPtr hwnd, int msg, uint wparam, uint lparam, IntPtr data);
// Communication functions
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpStartup(out int major, out int minor, out int level, out int translate, out int retransmit);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpCleanup();
[DllImport("wsnmp32.dll")]
public static extern IntPtr SnmpCreateSession(IntPtr hwnd, int msg, SnmpCallback callback, IntPtr data);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpClose(IntPtr session);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpRegister(IntPtr session, IntPtr src, IntPtr dest, IntPtr context, IntPtr notification, int state);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpListen(IntPtr entity, IntPtr status);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpSendMsg(IntPtr session, IntPtr src, IntPtr dest, IntPtr context, IntPtr pdu);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpRecvMsg(IntPtr session, out IntPtr src, out IntPtr dest, out IntPtr context, out IntPtr pdu);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpCancelMsg(IntPtr session, IntPtr reqid);
// Entity and Context functions
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpContextToStr(IntPtr context, ref SMIOCTETS octets);
[DllImport("wsnmp32.dll")]
public static extern IntPtr SnmpStrToContext(IntPtr session, ref SMIOCTETS octets);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpFreeEntity(IntPtr entity);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpEntityToStr(IntPtr entity, int size, IntPtr str);
[DllImport("wsnmp32.dll")]
public static extern IntPtr SnmpStrToEntity(IntPtr session, [MarshalAs(UnmanagedType.LPStr)] string str);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpFreeContext(IntPtr context);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpSetPort(IntPtr entity, int port);
// Database functions
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpGetRetransmitMode(out uint mode);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpGetRetry(IntPtr entity, out uint policy, out uint actual);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpGetTimeout(IntPtr entity, out int policy, out int actual);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpGetTranslateMode(out uint mode);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpGetVendorInfo(ref VENDORINFO Info);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpSetRetransmitMode(SNMPAPI_RETRANSMIT mode);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpSetRetry(IntPtr entity, int policy);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpSetTimeout(IntPtr entity, int policy);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpSetTranslateMode(SNMPAPI_TRANSLATE mode);
// PDU functions
[DllImport("wsnmp32.dll")]
public static extern IntPtr SnmpCreatePdu(IntPtr session, int type, int reqid, int status, int index, IntPtr vblist);
[DllImport("wsnmp32.dll")]
public static extern IntPtr SnmpDuplicatePdu(IntPtr session, IntPtr pdu);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpFreePdu(IntPtr pdu);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpGetPduData(IntPtr pdu, out int type, out int reqid, out int status, out int index, out IntPtr vblist);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpSetPduData(IntPtr pdu, ref int type, ref int reqid, ref int nonrepeaters, ref int maxreps, ref IntPtr vblist);
// Utility functions
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpDecodeMsg(IntPtr session, out IntPtr src, out IntPtr dest, out IntPtr context, out IntPtr pdu, ref SMIOCTETS octets);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpEncodeMsg(IntPtr session, IntPtr src, IntPtr dest, IntPtr context, IntPtr pdu, ref SMIOCTETS octets);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpFreeDescriptor(int syntax, ref SMIOID desc);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpFreeDescriptor(int syntax, ref SMIOCTETS desc);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpGetLastError(IntPtr session);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpOidCompare(IntPtr oid1, IntPtr oid2, int max, ref int result);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpOidCopy(IntPtr src, IntPtr dest);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpOidToStr(ref SMIOID oid, int size, IntPtr str);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpStrToOid(string str, ref SMIOID oid);
// Variable Binding functions
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpCountVbl(IntPtr vbl);
[DllImport("wsnmp32.dll")]
public static extern IntPtr SnmpCreateVbl(IntPtr session, ref SMIOID name, ref SMIVALUE value);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpDeleteVb(IntPtr vbl, int index);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpDuplicateVbl(IntPtr session, IntPtr vbl);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpFreeVbl(IntPtr vbl);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpGetVb(IntPtr vbl, int index, ref SMIOID name, ref SMIVALUE value);
[DllImport("wsnmp32.dll")]
public static extern SNMPAPI_STATUS SnmpSetVb(IntPtr vbl, int index, ref SMIOID name, ref SMIVALUE value);
}
}
 
Enums.cs
using System;
using System.Runtime.InteropServices;
namespace WinSnmp
{
/// <summary>
/// Snmp API enumerations
/// </summary>
public enum SNMPAPI_RETRANSMIT : int
{
SNMPAPI_OFF = 0,
SNMPAPI_ON = 1
}
public enum SNMPAPI_TRANSLATE : int
{
SNMPAPI_TRANSLATED = 0,
SNMPAPI_UNTRANSLATED_V1 = 1,
SNMPAPI_UNTRANSLATED_V2 = 2
}
public enum SNMPAPI_STATUS : uint
{
SNMPAPI_FAILURE = 0, /* Generic error code */
SNMPAPI_SUCCESS = 1, /* Generic success code */
/* WinSNMP API Error Codes (for SnmpGetLastError) */
/* (NOT SNMP Response-PDU error_status codes) */
SNMPAPI_ALLOC_ERROR = 2, /* Error allocating memory */
SNMPAPI_CONTEXT_INVALID = 3, /* Invalid context parameter */
SNMPAPI_CONTEXT_UNKNOWN = 4, /* Unknown context parameter */
SNMPAPI_ENTITY_INVALID = 5, /* Invalid entity parameter */
SNMPAPI_ENTITY_UNKNOWN = 6, /* Unknown entity parameter */
SNMPAPI_INDEX_INVALID = 7, /* Invalid VBL index parameter */
SNMPAPI_NOOP = 8, /* No operation performed */
SNMPAPI_OID_INVALID = 9, /* Invalid OID parameter */
SNMPAPI_OPERATION_INVALID = 10, /* Invalid/unsupported operation */
SNMPAPI_OUTPUT_TRUNCATED = 11, /* Insufficient output buf len */
SNMPAPI_PDU_INVALID = 12, /* Invalid PDU parameter */
SNMPAPI_SESSION_INVALID = 13, /* Invalid session parameter */
SNMPAPI_SYNTAX_INVALID = 14, /* Invalid syntax in smiVALUE */
SNMPAPI_VBL_INVALID = 15, /* Invalid VBL parameter */
SNMPAPI_MODE_INVALID = 16, /* Invalid mode parameter */
SNMPAPI_SIZE_INVALID = 17, /* Invalid size/length parameter */
SNMPAPI_NOT_INITIALIZED = 18, /* SnmpStartup failed/not called */
SNMPAPI_MESSAGE_INVALID = 19, /* Invalid SNMP message format */
SNMPAPI_HWND_INVALID = 20, /* Invalid Window handle */
SNMPAPI_OTHER_ERROR = 99, /* For internal/undefined errors */
/* Generic Transport Layer (TL) Errors */
SNMPAPI_TL_NOT_INITIALIZED = 100, /* TL not initialized */
SNMPAPI_TL_NOT_SUPPORTED = 101, /* TLdo
es not support protocol */
SNMPAPI_TL_NOT_AVAILABLE = 102, /* Network subsystem has failed */
SNMPAPI_TL_RESOURCE_ERROR = 103, /* TL resource error */
SNMPAPI_TL_UNDELIVERABLE = 104, /* Destination unreachable */
SNMPAPI_TL_SRC_INVALID = 105, /* Source endpoint invalid */
SNMPAPI_TL_INVALID_PARAM = 106, /* Input parameter invalid */
SNMPAPI_TL_IN_USE = 107, /* Source endpoint in use */
SNMPAPI_TL_TIMEOUT = 108, /* No response before timeout */
SNMPAPI_TL_PDU_TOO_BIG = 109, /* PDU too big for send/receive */
SNMPAPI_TL_OTHER = 199 /* Undefined TL error */
}
public enum SNMPAPI_PDU : int
{
SNMP_PDU_GET = 0xa0,
SNMP_PDU_GETNEXT = 0xa1,
SNMP_PDU_RESPONSE = 0xa2,
SNMP_PDU_SET = 0xa3,
SNMP_PDU_V1TRAP = 0xa4,
SNMP_PDU_GETBULK = 0xa5,
SNMP_PDU_INFORM = 0xa6,
SNMP_PDU_TRAP = 0xa7
}
public enum SNMPAPI_SYNTAX : int
{
// SNMP ObjectSyntax Values
SNMP_SYNTAX_SEQUENCE = 0x30,
// These values are used in the "syntax" member of the smiVALUE structure which follows
SNMP_SYNTAX_INT = 0x02,
SNMP_SYNTAX_INT32 = SNMP_SYNTAX_INT,
SNMP_SYNTAX_BITS = 0x03,
SNMP_SYNTAX_OCTETS = 0x04,
SNMP_SYNTAX_NULL = 0x05,
SNMP_SYNTAX_OID = 0x06,
SNMP_SYNTAX_IPADDR = 0x40,
SNMP_SYNTAX_CNTR32 = 0x41,
SNMP_SYNTAX_GAUGE32 = 0x42,
SNMP_SYNTAX_TIMETICKS = 0x43,
SNMP_SYNTAX_OPAQUE = 0x44,
SNMP_SYNTAX_NSAPADDR = 0x45,
SNMP_SYNTAX_CNTR64 = 0x46,
SNMP_SYNTAX_UINT32 = 0x47,
// Exception conditions in response PDUs for SNMPv2
SNMP_SYNTAX_NOSUCHOBJECT = 0x80,
SNMP_SYNTAX_NOSUCHINSTANCE = 0x81,
SNMP_SYNTAX_ENDOFMIBVIEW = 0x82
}
public enum SNMPAPI_ERROR_STATUS : int
{
SNMP_ERROR_NOERROR = 0,
SNMP_ERROR_TOOBIG = 1,
SNMP_ERROR_NOSUCHNAME = 2,
SNMP_ERROR_BADVALUE = 3,
SNMP_ERROR_READONLY = 4,
SNMP_ERROR_GENERR = 5,
SNMP_ERROR_NOACCESS = 6,
SNMP_ERROR_WRONGTYPE = 7,
SNMP_ERROR_WRONGLENGTH = 8,
SNMP_ERROR_WRONGENCODING = 9,
SNMP_ERROR_WRONGVALUE = 10,
SNMP_ERROR_NOCREATION = 11,
SNMP_ERROR_INCONSISTENTVALUE = 12,
SNMP_ERROR_RESOURCEUNAVAILABLE = 13,
SNMP_ERROR_COMMITFAILED = 14,
SNMP_ERROR_UNDOFAILED = 15,
SNMP_ERROR_AUTHORIZATIONERROR = 16,
SNMP_ERROR_NOTWRITABLE = 17,
SNMP_ERROR_INCONSISTENTNAME = 18
}
}
 
Structs.cs
using System;
using System.Runtime.InteropServices;
namespace WinSnmp
{
[StructLayout(LayoutKind.Sequential)]
public struct VENDORINFO
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
public string vendorName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
public string vendorContact;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string vendorVersionId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string vendorVersionDate;
public int vendorEnterprise;
}
[StructLayout(LayoutKind.Sequential)]
public struct SMIOCTETS
{
public uint size;
public IntPtr octets;
}
[StructLayout(LayoutKind.Sequential)]
public struct SMIOID
{
public uint size;
public IntPtr dwords;
}
[StructLayout(LayoutKind.Explicit)]
public struct SMIVUNION
{
[FieldOffset(0)] public int sNumber;
[FieldOffset(0)] public UInt32 uNumber;
[FieldOffset(0)] public Int64 hNumber;
[FieldOffset(0)] public SMIOCTETS str;
[FieldOffset(0)] public SMIOID oid;
[FieldOffset(0)] public byte empty;
}
[StructLayout(LayoutKind.Explicit)]
public struct SMIVALUE
{
[FieldOffset(0)] public SNMPAPI_SYNTAX type;
[FieldOffset(4)] public SMIVUNION val;
}
}
 
若需要Demo可发邮件给我。Codemoocow@163.com
 
后退
顶部