『QQ:1353814576』

SnmpSharpNet 实现简单打印机监控


这里是一个利用SnmpSharpNet 来实现获取打印机信息读取的范例 此处查询命令并不通用 需要替换成打印机厂商提供的查询协议命令

                OctetString vs = new OctetString("public");  
                AgentParameters parameters = new AgentParameters(vs);  
                parameters.Version = SnmpVersion.Ver2;  
                IpAddress ip = new IpAddress("192.168.20.220");  
                UdpTarget target = new UdpTarget((IPAddress)ip, 161, 2000, 1);  
                Pdu pdu = new Pdu(PduType.Get);  
                pdu.VbList.Add(".1.3.6.1.2.1.25.3.2.1.3.1");//打印机型号  
                pdu.VbList.Add(".1.3.6.1.2.1.25.3.2.1.5.1");//设备状态  
                pdu.VbList.Add(".1.3.6.1.2.1.25.3.5.1.1.1");//打印机状态  
                pdu.VbList.Add(".1.3.6.1.2.1.43.8.2.1.10.1.1");//手送进纸盒状态  
                pdu.VbList.Add(".1.3.6.1.2.1.43.8.2.1.10.1.2");//第一进纸盒状态  
                pdu.VbList.Add(".1.3.6.1.2.1.43.8.2.1.10.1.3");//第二进纸盒状态  
                pdu.VbList.Add(".1.3.6.1.2.1.25.3.5.1.2.1");//异常状态检测  
                pdu.VbList.Add(".1.3.6.1.4.1.1347.43.10.1.1.12.1.1");//总打印张数  
                pdu.VbList.Add(".1.3.6.1.4.1.1347.42.2.1.1.1.8.1.1");//A3彩色打印张数  
                pdu.VbList.Add(".1.3.6.1.4.1.1347.42.2.1.1.1.8.1.3");//A4彩色打印张数  
                pdu.VbList.Add(".1.3.6.1.4.1.1347.42.2.1.1.1.7.1.1");//A3黑白打印张数  
                pdu.VbList.Add(".1.3.6.1.4.1.1347.42.2.1.1.1.7.1.3");//A4黑白打印张数  
                pdu.VbList.Add(".1.3.6.1.2.1.43.11.1.1.8.1.1");//墨粉剩余(青色) 总使用量    
                pdu.VbList.Add(".1.3.6.1.2.1.43.11.1.1.9.1.1");//现存使用量  
                pdu.VbList.Add(".1.3.6.1.2.1.43.11.1.1.8.1.2");//墨粉剩余(品红色) 总使用量    
                pdu.VbList.Add(".1.3.6.1.2.1.43.11.1.1.9.1.2");//现存使用量  
                pdu.VbList.Add(".1.3.6.1.2.1.43.11.1.1.8.1.3");//墨粉剩余(黄色) 总使用量    
                pdu.VbList.Add(".1.3.6.1.2.1.43.11.1.1.9.1.3");//现存使用量  
                pdu.VbList.Add(".1.3.6.1.2.1.43.11.1.1.8.1.4");//墨粉剩余(黑色) 总使用量    
                pdu.VbList.Add(".1.3.6.1.2.1.43.11.1.1.9.1.4");//现存使用量  
                SnmpV2Packet result = (SnmpV2Packet)target.Request(pdu, parameters);  
                string s = result.Pdu.VbList[0].Value.ToString();  
                StringBuilder builder = new StringBuilder();  
                foreach (Vb v in result.Pdu.VbList)  
                {  
                    builder.AppendLine(v.Oid.ToString() + "***********" + v.Value + "\t\r\n");  
                }