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();
        }            

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM