- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Collections;
- namespace PortScan
- {
- class Program
- {
- // code from http://www.CodeSnippet.cn
- static void Main(string[] args)
- {
- IPAddress ip;
- int startPort, endPort;
- if (GetPortRange(args, out ip, out startPort, out endPort) == true) //提取命令行参数
- {
- Scan(ip, startPort, endPort); //端口扫描
- Console.ReadKey();
- }
- }
- #region 从命令行参数 中提取端口 + static bool GetPortRange(string[] args, out int startPort, out int endPort)
- /// <summary>
- /// 从命令行参数 中提取端口
- /// </summary>
- /// <param name="args">命令行参数</param>
- /// <param name="ip">输出 IP地址</param>
- /// <param name="startPort">输出 起始端口号</param>
- /// <param name="endPort">输出 终止端口号</param>
- /// <returns>提取成功返回true,否则返回false</returns>
- private static bool GetPortRange(string[] args, out IPAddress ip, out int startPort, out int endPort)
- {
- ip = null;
- startPort = endPort = 0;
- //帮助 命令
- if (args.Length != 0 && (args[0] == "/?" || args[0] == "/help"))
- {
- Console.WriteLine("Scan port from startPort to endPort of the host specified by the IPAddress.");
- Console.WriteLine("Command Format:");
- Console.WriteLine("PortScan IPAddress startPort endPort");
- Console.WriteLine("For example:");
- Console.WriteLine("PortScan 127.0.0.1 1 1024");
- return false;
- }
- if (args.Length == 3)
- {
- //解析端口号成功
- if (IPAddress.TryParse(args[0], out ip) && int.TryParse(args[1], out startPort) && int.TryParse(args[2], out endPort))
- {
- return true;
- }
- else
- {
- Console.WriteLine("参数格式不正确!");
- return false;
- }
- }
- else
- {
- Console.WriteLine("参数数目不正确!");
- return false;
- }
- }
- #endregion
- /// <summary>
- /// 端口 扫描
- /// </summary>
- /// <param name="ip">扫描的 IP地址</param>
- /// <param name="startPort">起始端口号</param>
- /// <param name="endPort">终止端口号</param>
- static void Scan(IPAddress ip, int startPort, int endPort)
- {
- Random rand = new Random((int)DateTime.Now.Ticks);
- Console.WriteLine("Begin Scan...");
- for (int port = startPort; port < endPort; port++)
- {
- Socket scanSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
- //寻找一个未使用的端口进行绑定
- do
- {
- try
- {
- scanSocket.Bind(new IPEndPoint(IPAddress.Any, rand.Next(65535)));
- break;
- }
- catch
- {
- //绑定失败
- }
- } while (true);
- try
- {
- scanSocket.BeginConnect(new IPEndPoint(ip, port), ScanCallBack, new ArrayList() { scanSocket, port });
- }
- catch
- {
- // Console.WriteLine("port {0,5}\\tClosed.\\n{1}", port, ex.Message);
- continue;
- }
- }
- Console.WriteLine("Port Scan Completed!");
- }
- /// <summary>
- /// BeginConnect的回调函数
- /// </summary>
- /// <param name="result">异步Connect的结果</param>
- static void ScanCallBack(IAsyncResult result)
- {
- //解析 回调函数输入 参数
- ArrayList arrList = (ArrayList)result.AsyncState;
- Socket scanSocket = (Socket)arrList[0];
- int port = (int)arrList[1];
- //判断端口是否开放
- if (result.IsCompleted && scanSocket.Connected)
- {
- Console.WriteLine("port {0,5}\\tOpen.", port);
- }
- else
- {
- //Console.WriteLine("port {0,5}\\tClosed.", port);
- }
- //关闭套接字
- scanSocket.Close();
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/131220138022.html
来源: http://www.codesnippet.cn/detail/131220138022.html