我正在使用 SharpSnmpLib 创建一个 snmp 服务器和客户端。我已经让 V1 和 V2 使用下面的代码,但在 V3 上遇到问题,因为它返回了格式错误的消息。
我的服务器为每个设备创建一个侦听器。这是我的服务器:
GetMessageHandler handler = new GetMessageHandler();
GetBulkMessageHandler bulkMessageHandler = new GetBulkMessageHandler();
HandlerMapping hm = new HandlerMapping("v1", "GET", handler);
HandlerMapping hm2 = new HandlerMapping("v1", "GETNEXT", handler);
HandlerMapping hm3 = new HandlerMapping("v2", "GET", handler);
HandlerMapping hm4 = new HandlerMapping("v2", "GETNEXT", handler);
HandlerMapping hm5 = new HandlerMapping("v2, v3", "GETBULK", handler);
HandlerMapping hm6 = new HandlerMapping("v3", "GETBULK", bulkMessageHandler);
List<HandlerMapping> hmList = new List<HandlerMapping> {hm, hm2, hm3, hm4, hm5, hm6};
List <IMembershipProvider> memberships = new List<IMembershipProvider>();
MessageHandlerFactory mhf = new MessageHandlerFactory(hmList.ToArray());
Version1MembershipProvider v1 = new Version1MembershipProvider(new OctetString(community), new OctetString(community));
Version2MembershipProvider v2 = new Version2MembershipProvider(new OctetString(community), new OctetString(community));
Version3MembershipProvider v3 = new Version3MembershipProvider();
memberships.Add(v1);
memberships.Add(v2);
memberships.Add(v3);
ComposedMembershipProvider membershipProviders = new ComposedMembershipProvider(memberships.ToArray());
SnmpApplicationFactory factory = new SnmpApplicationFactory(store, membershipProviders, mhf);
EngineGroup eg = new EngineGroup();
Listener l = new Listener();
l.MessageReceived += l_MessageReceived;
l.ExceptionRaised += l_ExceptionRaised;
engine = new SnmpEngine(factory, l, eg);
engine.ExceptionRaised += engine_ExceptionRaised;
engine.Listener.ClearBindings();
engine.Listener.AddBinding(new System.Net.IPEndPoint("127.0.0.1", 161));
engine.Start();
这是我的客户:
private IPrivacyProvider CreateAuthProvider()
{
Levels securityLevel = Levels.Authentication | Levels.Privacy | Levels.Reportable;
IAuthenticationProvider auth = MD5AuthenticationProvider(new OctetString("abc1234"));
IPrivacyProvider privAuth;
if ((securityLevel & Levels.Privacy) == Levels.Privacy)
{
if (DESPrivacyProvider.IsSupported)
{
privAuth = new DESPrivacyProvider(new OctetString("abc1234"), auth);
}
else
{
privAuth = null;
}
}
else
{
privAuth = new DefaultPrivacyProvider(auth);
}
return privAuth;
}
IPrivacyProvider privAuth;
privAuth = CreateAuthProvider();
Discovery discoveryAuth = Messenger.GetNextDiscovery(SnmpType.GetBulkRequestPdu);
ReportMessage reportMessgae = discoveryAuth.GetResponse(config.Timeout, new IPEndPoint(127.0.0.1, 161));
var serverEndPoint = new IPEndPoint(127.0.0.1, 161);
GetBulkRequestMessage message = new GetBulkRequestMessage(VersionCode.V3,
0,
0,
new OctetString("newUser"),
0,
10,
new ObjectIdentifier("1.3.6.1.4.1.37805.1.1.0.1"),
privAuth,
Messenger.MaxMessageSize,
reportMessgae);
ISnmpMessage response = message.GetResponse(10000, serverEndPoint);
if (response.Pdu().ErrorStatus.ToInt32() != 0)
{
throw ErrorException.Create(
"Error in response of SNMP GET Bulk Request for V3",
address,
response);
}
如果有人能解释为什么这会很好,我可以清楚地得到 V1 和 V2 数据,但没有得到任何 V3 数据。当我为 V3 进行调试时,连接似乎是活动的,但我没有正确地做某事(也许是解密?)