如何在 C# 中订阅 snmpclient(打印机)

Posted

技术标签:

【中文标题】如何在 C# 中订阅 snmpclient(打印机)【英文标题】:How to subscribe for the snmpclient (printer) in C# 【发布时间】:2021-03-18 06:29:08 【问题描述】:

实际上我有一个示例 SNMP 陷阱接收器代码。但它就像建立了一个 UDP 套接字并侦听陷阱。我正在通过在 exe 的帮助下生成陷阱来手动测试代码。但现在我必须订阅我需要从中接收 Trap 的 snmp 客户端(打印机)。 谁能帮我写一个订阅代码,从监听器代码订阅打印机。

snmptraplistener 代码:

class Program 
    static void Main(string[] args) 
        // Construct a socket and bind it to the trap manager port 162
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 162);
        EndPoint ep = (EndPoint)ipep;
        socket.Bind(ep);
        // Disable timeout processing. Just block until packet is received
        socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0);
        bool run = true;
        int inlen = -1;
        while (run) 
            byte[] indata = new byte[16 * 1024];
            // 16KB receive buffer int inlen = 0;
            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) 
                // Check protocol version int
                int ver = SnmpPacket.GetProtocolVersion(indata, inlen);
                if (ver == (int)SnmpVersion.Ver1) 
                    // 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);
                    Console.WriteLine("*** Trap specific: 0", pkt.Pdu.Specific);
                    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) 
                        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 
                    // Parse SNMP Version 2 TRAP packet
                    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】:

一般来说,你问的不是编程问题,因为你的 Trap 监听器只能监听 162 端口的 UDP/SNMP 数据包,没有办法订阅任何东西。

打印机管理员有责任在该打印机上配置 SNMP 代理以将陷阱发送到您的侦听器(IP/端口)。

【讨论】:

以上是关于如何在 C# 中订阅 snmpclient(打印机)的主要内容,如果未能解决你的问题,请参考以下文章

C#如何订阅用户控件事件/用户控件没有实例名称

如何在C#中打印类的道具名称[重复]

C#中如何打印出整个PANEL的内容?

在 C# 中打印和打印预览位图

C#利用RabbitMQ实现消息订阅与发布

C# 编程指南-事件