所以我正在构建一个 snmp 陷阱接收器来接收 snmp 陷阱消息。我正在使用来自 lexstudios 的 sharpsnmp-net 包以及来自 github 的带有 pipline 的 sample.engine。我正在创建一个简单的 wpf 应用程序,仅在屏幕上显示消息以测试 .net 4.7.1 中的所有内容。
所以对于听力部分,我粗略地使用了示例 snmpd。
public partial class MainWindow : Window
{
private SnmpEngine _engine;
private const string StrAllUnassigned = "All Unassigned";
public MainWindow()
{
var store = new ObjectStore();
store.Add(new SysDescr());
store.Add(new SysObjectId());
store.Add(new SysUpTime());
store.Add(new SysContact());
store.Add(new SysName());
store.Add(new SysLocation());
store.Add(new SysServices());
store.Add(new SysORLastChange());
store.Add(new SysORTable());
store.Add(new IfNumber());
store.Add(new IfTable());
var users = new UserRegistry();
users.Add(new OctetString("neither"), DefaultPrivacyProvider.DefaultPair);
users.Add(new OctetString("authen"), new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication"))));
if (DESPrivacyProvider.IsSupported)
{
users.Add(new OctetString("privacy"), new DESPrivacyProvider(new OctetString("privacyphrase"),
new MD5AuthenticationProvider(new OctetString("authentication"))));
}
if (AESPrivacyProviderBase.IsSupported)
{
users.Add(new OctetString("aes"), new AESPrivacyProvider(new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication"))));
users.Add(new OctetString("aes192"), new AES192PrivacyProvider(new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication"))));
users.Add(new OctetString("aes256"), new AES256PrivacyProvider(new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication"))));
}
var getv1 = new GetV1MessageHandler();
var getv1Mapping = new HandlerMapping("v1", "GET", getv1);
var setv1 = new SetV1MessageHandler();
var setv1Mapping = new HandlerMapping("v1", "SET", setv1);
var getnextv1 = new GetNextV1MessageHandler();
var getnextv1Mapping = new HandlerMapping("v1", "GETNEXT", getnextv1);
var v1 = new Version1MembershipProvider(new OctetString("public"), new OctetString("public"));
var membership = new ComposedMembershipProvider(new IMembershipProvider[] { v1 });
var handlerFactory = new MessageHandlerFactory(new[]
{
getv1Mapping,
setv1Mapping,
getnextv1Mapping
});
var pipelineFactory = new SnmpApplicationFactory(new RollingLogger(), store, membership, handlerFactory);
_engine = new SnmpEngine(pipelineFactory, new Listener { Users = users }, new EngineGroup());
_engine.ExceptionRaised += (sender, e) => MessageBox.Show(e.Exception.ToString());
InitializeComponent();
txtIp.Text = @"162"; //port to receive snmp trap
cmbIp.Items.Add(StrAllUnassigned);
foreach (IPAddress address in Dns.GetHostEntry(string.Empty).AddressList.Where(address => !address.IsIPv6LinkLocal))
{
cmbIp.Items.Add(address);
}
cmbIp.SelectedIndex = 0;
}
public void StartListeners()
{
try
{
_engine.Listener.ClearBindings();
int port = int.Parse(txtIp.Text, CultureInfo.InvariantCulture);
/*
if (cmbIp.Text == StrAllUnassigned)
{
if (Socket.OSSupportsIPv4)
{
_engine.Listener.AddBinding(new IPEndPoint(IPAddress.Any, port));
}
if (Socket.OSSupportsIPv6)
{
_engine.Listener.AddBinding(new IPEndPoint(IPAddress.IPv6Any, port));
}
_engine.Start();
if (_engine.Active)
{
MessageBox.Show("Engine activated");
}
return;
}
*/
IPAddress address = IPAddress.Parse(cmbIp.Text);
if (address.AddressFamily == AddressFamily.InterNetwork)
{
if (!Socket.OSSupportsIPv4)
{
MessageBox.Show(Listener.ErrorIPv4NotSupported);
return;
}
_engine.Listener.AddBinding(new IPEndPoint(address, port));
_engine.Listener.MessageReceived += Listener_MessageReceived;
_engine.Start();
if (_engine.Active)
{
MessageBox.Show("Engine activated");
}
return;
}
if (!Socket.OSSupportsIPv6)
{
MessageBox.Show(Listener.ErrorIPv6NotSupported);
return;
}
_engine.Listener.AddBinding(new IPEndPoint(address, port));
_engine.Start();
}
catch (Exception ex)
{
MessageBox.Show("Exception has been thrown in start: " + ex);
}
}
private void Listener_MessageReceived(object sender, MessageReceivedEventArgs e)
{
MessageBox.Show("message received");
}
private void StopListeners()
{
_engine.Stop();
_engine.Dispose();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//send trap
IPAddress ip = IPAddress.Parse("127.0.0.1");
Messenger.SendTrapV1(
new IPEndPoint(ip, 162),
IPAddress.Loopback, // here should be IP of the current machine.
new OctetString("public"),
new ObjectIdentifier(new uint[] { 1, 3, 6 }),
GenericCode.ColdStart,
0,
0,
new List<Variable>());
}
private void Start_Click(object sender, RoutedEventArgs e)
{
try
{
StartListeners();
}
catch (PortInUseException ex)
{
MessageBox.Show(@"Port is already in use: " + ex.Endpoint, @"Error");
}
}
private void stop_Click(object sender, RoutedEventArgs e)
{
if (_engine.Active)
{
try
{
_engine.Listener.MessageReceived -= Listener_MessageReceived;
StopListeners();
}
catch (Exception ex)
{
MessageBox.Show("exception in stop: " + ex);
}
}
}
}
因此,要接收 snmp 陷阱消息,您还必须发送一个。我有一个带有 IP 地址的端口号组合框的文本框。发送陷阱按钮和启动和停止按钮。
发送陷阱效果很好我可以在wireshark中验证我确实有一个snmp数据包但是icmp回答端口不可达,这意味着没有人在听。->问题1
激活引擎时,我没有任何错误,但关于 icmp,我使用以下命令在 cmd 中签入:netstat -an 检查端口 162 是否正在侦听,但运气不好。
我如何验证引擎是否正常工作或有什么东西在听?我订阅了 message_received 事件,但我没有收到任何可能导致没有人在听的事实。
最后但并非最不重要的一点是,当我按下停止按钮时,我确实收到了一个套接字异常,但没有从 try catch 循环中收到我自己的任何消息 - > 我应该假设这是在包中吗?
如何解决这个问题呢。到目前为止,我只想接收 snmpv1 陷阱消息并将它们存储在数据表中。