C#使用SnmpSharpNet接收snmp的trap消息示例代码

using System;
using System.Net;
using System.Net.Sockets;
using SnmpSharpNet;
namespace traprecv {
	class Program {
		static void Main(string[] args) {
			Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
			IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 162); //162是snmp的trap上报端口
			EndPoint ep = (EndPoint)ipep;
			socket.Bind(ep);
			socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0);
			bool run = true;
			int inlen = -1;
			while (run) {
				byte[] indata = new byte[16 * 1024];
				IPEndPoint peer = new IPEndPoint(IPAddress.Any, 0);
				EndPoint inep = (EndPoint)peer;
				try {
					inlen = socket.ReceiveFrom(indata, ref inep);
				}
			 	catch( Exception ex ) {
					Console.WriteLine("Exception {0}", ex.Message);
					inlen = -1;
				}
			 	if (inlen > 0) {
					int ver = SnmpPacket.GetProtocolVersion(indata, inlen);
					if (ver == (int)SnmpVersion.Ver1) { //snmp版本1
						// Parse SNMP Version 1 TRAP packet 
						SnmpV1TrapPacket pkt = new SnmpV1TrapPacket();
						pkt.decode(indata, inlen);
						Console.WriteLine("** SNMP Version 1 TRAP received from {0}:", inep.ToString());
						Console.WriteLine("*** Trap generic: {0}", pkt.Pdu.Generic); //snmp的pdu类型,在0-6之间的值表示:标准的trap类型的一种,如果这个值是6表示是厂家自定义trap类型,这时可以通过Pdu.Specific来判断是厂家自定义的哪种trap类型
						Console.WriteLine("*** Trap specific: {0}", pkt.Pdu.Specific); //厂家自定义trap
						Console.WriteLine("*** Agent address: {0}", pkt.Pdu.AgentAddress.ToString());
						Console.WriteLine("*** Timestamp: {0}", pkt.Pdu.TimeStamp.ToString());
						Console.WriteLine("*** VarBind count: {0}", pkt.Pdu.VbList.Count);
						Console.WriteLine("*** VarBind content:");
						foreach (Vb v in pkt.Pdu.VbList) {  //遍历输出snmp的pdu变量
							Console.WriteLine("**** {0} {1}: {2}", v.Oid.ToString(), SnmpConstants.GetTypeName(v.Value.Type), v.Value.ToString());
						}
						Console.WriteLine("** End of SNMP Version 1 TRAP data.");
					} else {
						//解析snmp版本2数据包
						SnmpV2Packet pkt = new SnmpV2Packet();
						pkt.decode(indata, inlen);
						Console.WriteLine("** SNMP Version 2 TRAP received from {0}:", inep.ToString());
						if ((SnmpSharpNet.PduType)pkt.Pdu.Type != PduType.V2Trap) {
							Console.WriteLine("*** NOT an SNMPv2 trap ****");
						} else {
							Console.WriteLine("*** Community: {0}", pkt.Community.ToString());
							Console.WriteLine("*** VarBind count: {0}", pkt.Pdu.VbList.Count);
							Console.WriteLine("*** VarBind content:");
							foreach (Vb v in pkt.Pdu.VbList) {
								Console.WriteLine("**** {0} {1}: {2}", 
								   v.Oid.ToString(), SnmpConstants.GetTypeName(v.Value.Type), v.Value.ToString());
							}
							Console.WriteLine("** End of SNMP Version 2 TRAP data.");
						}
					}
				} else {
					if (inlen == 0)
						Console.WriteLine("Zero length packet received.");
				}
			}
		}
	}
}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值