C#扫描指定IP端口
来源:互联网 发布:python 顶级黑客 编辑:程序博客网 时间:2024/06/01 13:11
//===========================================================// 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--; } } }}
原文地址: http://www.cnblogs.com/wugang/archive/2012/10/26/2740885.html
- C#扫描指定IP端口
- Python 扫描IP段 指定端口是否开放
- C#扫描计算机端口
- 自动扫描局域网并且发现指定端口开放的主机ip的shell脚本
- C#扫描IP
- C#多线程扫描IP段和端口段.exe 小黄人软件定制
- windows指定ip,端口开放
- C++扫描指定主机开放的端口
- 使用c#打开指定端口
- c#获取服务器IP、端口
- 远程登陆MYSQL-指定ip和端口号
- 远程登陆MySQL-指定ip和端口号
- 远程登陆MySQL-指定ip和端口号
- 远程登陆MySQL-指定ip和端口号
- C#动态扫描指定目录文件
- java扫描指定主机的端口socket服务
- Linux防火墙限制指定端口只能由指定IP访问
- firewalld对指定IP开放指定端口的配置
- Access数据库表导入sql2000数据库
- csdn飞鸽传书 打了七天七夜
- asp.net页面类
- 抽象类与接口的区别(整理)
- 让Category支持添加属性与成员变量
- C#扫描指定IP端口
- Linux内核数据结构之链表
- linux sort,uniq,cut,wc命令详解
- StartActivity ,onActivityResult,setResult简单运用
- 敏捷开发中的Code Review
- BlazdDS里面一些jar包的功能描述
- Java和MySQL数据类型对应一览
- hdu 4712 2种解发(状态dp| 随机化) 我又相信爱情了~~(第一发)
- FileItem类的常用方法