コード例 #1
0
        public static List <Testing.DataOfPC> ScanNetwork()
        {
            string command    = "/C net view /all";
            string outputDump = Testing.ViewCommandLineResult(command);

            var items = Regex.Split(outputDump, @"\\");

            List <string> FullList = new List <string>();
            int           i        = 2;

            while (i < items.Length)
            {
                /*
                 * Seperating the list of PC's from the output string to get only the PC names.
                 */
                var s = items[i];

                /*
                 * Sometimes we get the Remark of some PC's , so inorder to avoid them from getting
                 * into the main list regex is used as space and its seperated.
                 */
                var Name = Regex.Split(s, @" ");
                FullList.Add(Name[0]);
                i = i + 2;
            }

            int            n   = FullList.ToArray().Length;
            List <int>     l   = new List <int>();
            List <Testing> ans = new List <Testing>();
            int            flag;

            for (flag = 0; flag < n; flag += 4)
            {
                l.Add(flag);
            }
            l.Add(n);
            var arr = l.ToArray();

            flag = 0;


            while (flag < arr.Length - 1)
            {
                ManualResetEvent[] doneEvents = new ManualResetEvent[arr[flag + 1] - arr[flag]];
                Testing[]          testArray  = new Testing[arr[flag + 1] - arr[flag]];

                int    counter = 0;
                string NameOfPC;
                var    FullListArray = FullList.ToArray();
                //var done = new CountdownEvent(4);

                for (i = arr[flag]; i < arr[flag + 1]; i++)
                {
                    NameOfPC  = "\\";
                    NameOfPC += FullListArray[i];
                    command   = "/C net view \\";
                    command  += NameOfPC;

                    doneEvents[counter] = new ManualResetEvent(false);
                    //done.AddCount();
                    Testing t = new Testing(FullList, command, i, doneEvents[counter]);
                    testArray[counter] = t;

                    counter += 1;
                    ThreadPool.QueueUserWorkItem(t.ThreadPoolCallback, i);
                }

                //WaitHandle.WaitAll(doneEvents);
                //done.Signal();
                //done.Wait();
                foreach (WaitHandle handle in doneEvents)
                {
                    handle.WaitOne();
                }
                flag += 1;
            }
            return(Testing.global.Result);
        }
コード例 #2
0
        public static List <Testing.DataOfPC> ScanNetwork()
        {
            string        command    = "/C arp -a";
            string        outputDump = Testing.ViewCommandLineResult(command);
            List <string> FullList   = new List <string>();

            // Iterate all other PCs
            foreach (Match match in Regex.Matches(outputDump, @"([0-9\.]+){4} [^\-]"))
            {
                Console.WriteLine(match.ToString().Trim());
                //Console.WriteLine(Regex.IsMatch(match.ToString(), @"2[2-5][4-5]\.([0-9\.]+){3}"));
                //Console.WriteLine(Regex.IsMatch(match.ToString(), @"([0-9\.]+){3}\.255"));
                if (Regex.IsMatch(match.ToString().Trim(), @"2[2-5][4-5]\.([0-9\.]+){3}") || Regex.IsMatch(match.ToString().Trim(), @"([0-9\.]+){3}\.255") || Regex.IsMatch(match.ToString().Trim(), @"([0-9\.]+){3}\.0"))
                {
                    Console.WriteLine("This is a multicast address range");
                }
                else
                {
                    FullList.Add(match.ToString().Trim());
                }
            }

            // Adds owner host to the list of PCs
            FullList.Add(Dns.GetHostName().ToString());

            int            n   = FullList.ToArray().Length;
            List <int>     l   = new List <int>();
            List <Testing> ans = new List <Testing>();
            int            flag;

            for (flag = 0; flag < n; flag += 4)
            {
                l.Add(flag);
            }
            l.Add(n);
            var arr = l.ToArray();

            flag = 0;


            while (flag < arr.Length - 1)
            {
                ManualResetEvent[] doneEvents = new ManualResetEvent[arr[flag + 1] - arr[flag]];
                Testing[]          testArray  = new Testing[arr[flag + 1] - arr[flag]];

                int    counter = 0;
                string NameOfPC;
                var    FullListArray = FullList.ToArray();
                //var done = new CountdownEvent(4);
                int i;
                for (i = arr[flag]; i < arr[flag + 1]; i++)
                {
                    NameOfPC  = "\\";
                    NameOfPC += FullListArray[i];
                    command   = "/C net view \\";
                    command  += NameOfPC;

                    doneEvents[counter] = new ManualResetEvent(false);
                    //done.AddCount();
                    Testing t = new Testing(FullList, command, i, doneEvents[counter]);
                    testArray[counter] = t;

                    counter += 1;
                    ThreadPool.QueueUserWorkItem(t.ThreadPoolCallback, i);
                }

                //WaitHandle.WaitAll(doneEvents);
                //done.Signal();
                //done.Wait();
                foreach (WaitHandle handle in doneEvents)
                {
                    handle.WaitOne();
                }
                flag += 1;
            }
            return(Testing.global.Result);
        }