Snmp协议应用-扫描局域网内打印机

Snmp协议应用-扫描局域网内打印机

Snmp协议简单介绍

    简单网络管理协议(SNMP),由一组网络管理的标准组成,包含一个应用层协议(application layer protocol)、数据库模型(database schema)和一组资源对象。该协议能够支持网络管理系统,用以监测连接到网络上的设备是否有任何引起管理上关注的情况。该协议是互联网工程工作小组(IETF,Internet Engineering Task Force)定义的internet协议簇的一部分。SNMP的目标是管理互联网Internet上众多厂家生产的软硬件平台,因此SNMP受Internet标准网络管理框架的影响也很大。SNMP已经出到第三个版本的协议,其功能较以前已经大大地加强和改进了。

    详细信息可网上查找,参考http://www.cnblogs.com/zhangsf/archive/2013/08/26/3283124.html

代码部分

    代码在Linux虚拟机(1cpu、1GB内存)上运行较慢,将for替换为Parallel后速度有所提升。

public class ScanIpAddress
    {
        CountdownEvent countdown = null;
        int region = 0;
        string[] oids = null;
        public Dictionary<string, List<string>> dicOid = null;
        public ScanIpAddress(int region,string[] oids)
        {
            this.region = region;
            this.oids = oids;
            this.dicOid = new Dictionary<string, List<string>>();
        }
        private bool getSnmp(string host)
        {
            bool result = false;
            /* Get an SNMP Object
            */
            SimpleSnmp snmpVerb = new SimpleSnmp(host, 161, "public", 100, 0);
            if (!snmpVerb.Valid)
            {
                Console.WriteLine("Seems that IP or comunauty is not cool");
                return result;
            }

            //Oid varbind = new Oid(OID);

            Dictionary<Oid, AsnType> snmpDataS = snmpVerb.Get(SnmpVersion.Ver1, this.oids);
            if (snmpDataS != null)
            {
                List<string> oidList = new List<string> ();
                foreach (var item in snmpDataS)
                {
                    oidList.Add(item.Value.ToString());
                }
                this.dicOid.Add(host, oidList);
                result = true;
            }

            return result;
        }

        private void p_PingCompleted(object sender, PingCompletedEventArgs e)
        {
            string ip = (string)e.UserState;
            if (e.Reply != null && e.Reply.Status == IPStatus.Success)
            {
                /* PRINTER-PORT-MONITOR-MIB - 1.3.6.1.4.1.2699
                 * ppmPrinterIEEE1284DeviceId: 1.3.6.1.4.1.2699.1.2.1.2.1.1.3
                 */
                getSnmp(ip);
            }
            else if (e.Reply == null)
            {
                Console.WriteLine("Pinging {0} failed. (Null Reply object?)", ip);
            }
            countdown.Signal();
        }

        public void ScanPrinters()
        {
            Console.WriteLine("ScanPrinters");
            countdown = new CountdownEvent(1);
            Stopwatch sw = new Stopwatch();
            sw.Start();
            string ipBase = "192.168.{0}.{1}";
            Parallel.For(0, 256, (i) =>
            {
                string ip = string.Format(ipBase, region, i);
                Console.WriteLine(ip);
                Ping p = new Ping();
                p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
                countdown.AddCount();
                p.SendAsync(ip, 100, ip);
            });
            //for (int i = 0; i <= 255; i++)
            //{
            //    string ip = string.Format(ipBase, region, i);
            //    Console.WriteLine(ip);
            //    Ping p = new Ping();
            //    p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
            //    countdown.AddCount();
            //    p.SendAsync(ip, 100, ip);
            //}
            countdown.Signal();
            countdown.Wait();
            sw.Stop();
            Console.WriteLine("共耗时(毫秒):" + sw.ElapsedMilliseconds);
            Console.WriteLine("Printer scan finished");
        }
    }

    调用部分:

    class Program
    {
        public static string oIDProductName = "1.3.6.1.2.1.25.3.2.1.3.1";//  "1.3.6.1.2.1.43.5.1.1.16.1";
        public static string oIDSerialNumber = "1.3.6.1.2.1.43.5.1.1.17.1";
        static void Main(string[] args)
        {
            List<string> list = new List<string>();
            list.Add(oIDProductName);
            list.Add(oIDSerialNumber);

            ScanIpAddress sip = new ScanIpAddress(100, list.ToArray());
            sip.ScanPrinters();
            if (sip.dicOid != null && sip.dicOid.Count > 0)
            {
                foreach (var item in sip.dicOid)
                {
                    Console.WriteLine(item.Key);
                    List<string> oidList = item.Value;
                    string printName = string.Empty;
                    foreach (var str in oidList)
                    {
                        printName += str.Replace(" ", "");
                        Console.WriteLine(str);
                    }
                    string [] arr = item.Key.Split('.');
                    //CupsPrint.AddPrint(arr[2] + arr[3], item.Key);
                }
            }
            Console.ReadLine();
        }            

 

posted @ 2016-12-08 16:46  ddrsql  阅读(3599)  评论(0编辑  收藏  举报