C#掃描指定IP端口


//===========================================================
//    C# 實現端口掃描
//===========================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

using System.Threading;

namespace ConApp
{
    class Program
    {
        //已掃描端口數目
        internal static int scannedCount = 0;

        internal static int runningThreadCount = 0;

        internal static List<int> openedPorts = new List<int>();

        static int startPort = 1;
        static int endPort = 500;

        static int maxThread = 100;


        static void Main(string[] args)
        {
            //簡單提示
            Console.WriteLine("////////////////////////////////////////////////////////////////////////////////////");
            Console.WriteLine("//   Writer;Feeling");
            Console.WriteLine("////////////////////////////////////////////////////////////////////////////////////");
            Console.WriteLine("請輸入要掃描的主機;");
            string host = Console.ReadLine();
            Console.WriteLine("請輸入掃描的端口 例如:1-800");
            string portRange = Console.ReadLine();
            startPort = int.Parse(portRange.Split('-')[0].Trim());
            endPort = int.Parse(portRange.Split('-')[1].Trim());

            for (int port = startPort; port < endPort; port++)
            {
                Scanner scanner = new Scanner(host, port);
                Thread thread = new Thread(new ThreadStart(scanner.Scan));
                thread.Name = port.ToString();
                thread.IsBackground = true;
                thread.Start();

                runningThreadCount++;
                Thread.Sleep(10);

                //循環,直到某個線程工作完畢才啟動另一新線程,也可以叫做推拉窗技術
                while (runningThreadCount >= maxThread) ;
            }
            //空循環,直到所有端口掃描完畢
            while (scannedCount + 1 < (endPort - startPort)) ;
            Console.WriteLine();
            Console.WriteLine();

            //輸出結果
            Console.WriteLine("Scan for host:{0} has been completed, \n total {1} ports scanned, \n opened ports:{2}", host, (endPort - startPort), openedPorts.Count);

            foreach (int port in openedPorts)
            {
                Console.WriteLine("\tport: {0} is open", port.ToString().PadLeft(6));
            }

            Console.ReadLine();

        }
    }

    class Scanner
    {
        string m_host;
        int m_port;

        public Scanner(string host, int port)
        {
            m_host = host;
            m_port = port;
        }
        public void Scan()
        {
            TcpClient tc = new TcpClient();
            tc.SendTimeout = tc.ReceiveTimeout = 2000;

            try
            {
                tc.Connect(m_host, m_port);
                if (tc.Connected)
                {
                    Console.WriteLine("Port {0} is Open", m_port.ToString().PadRight(6));
                    Program.openedPorts.Add(m_port);
                }
            }
            catch
            {
                Console.WriteLine("Port {0} is Closed", m_port.ToString().PadRight(6));
            }
            finally
            {
                tc.Close();
                tc = null;
                Program.scannedCount++;
                Program.runningThreadCount--;
            }
        }

    }
} 

 


免責聲明!

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



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