private static void RunScan(IEnumerable <string> range, IPhelper ipHelper) { //using (countdown = new CountdownEvent(1)) //{ foreach (string ip in range) { if (ipHelper.Ping(ip)) { Console.ForegroundColor = ConsoleColor.DarkYellow; _writter.WriteLine($"Starting scan for IP: {ip} @ {DateTime.Now:yyyy.MM.dd HH:mm:sss}"); //countdown.TryAddCount(); using (PortScanner pScanner = new PortScanner(ip, StartPort, EndPort, _writter)) { pScanner.StartWork(_maxThread); } } else { Console.ForegroundColor = ConsoleColor.DarkRed; _writter.WriteLine($"Host {ip} is not active @ {DateTime.Now:yyyy.MM.dd HH:mm:sss}"); } } // while (countdown.Signal()) // { // countdown.Wait(); // } //} }
private static void RunScanParallel(IEnumerable <string> range, IPhelper ipHelper) { //using (countdown = new CountdownEvent(1)) //{ Parallel.ForEach(range, ip => { if (ipHelper.Ping(ip)) { //countdown.TryAddCount(); Console.ForegroundColor = ConsoleColor.DarkYellow; _writter.WriteLine($"Starting scan for IP: {ip} @ {DateTime.Now:yyyy.MM.dd HH:mm:sss}"); PortScanner pScanner = new PortScanner(ip, StartPort, EndPort, _writter); pScanner.StartWork(_maxThread); }
static void Main(string[] args) { var commandLineParser = CommandLineOptions.Setup(); var result = commandLineParser.Parse(args); if (CommandLineOptions.HandleStandardResult(result)) { return; } _commandLineOptions = commandLineParser.Object; CreateLocation(_commandLineOptions.OutputLocation); IPhelper ipHelper = new IPhelper(); _writter = new Writter(WriteMode.Both, _outputFile); _writter.WriteLine($"Start application {DateTime.Now:yyyy.MM.dd HH:mm:sss}"); _writter.WriteLine(Environment.NewLine); _writter.WriteLine($"Output location: {_outputFile}"); _startAddress = ipHelper.GetIPaddress(_commandLineOptions.StartIpAddress); _writter.WriteLine($"Start IP address {_startAddress}"); StartPort = SetPort(_commandLineOptions.StartPort); EndPort = SetPort(_commandLineOptions.EndPort); _endAddress = ipHelper.GetIPaddress(_commandLineOptions.EndIpAddress); _writter.WriteLine($"End IP address {_endAddress}"); _maxThread = _commandLineOptions.MaxThreadCount; _writter.WriteLine($"Maximum threads allocated {_maxThread}"); _writter.WriteLine($"Starting port {StartPort}"); _writter.WriteLine($"Ending port {EndPort}"); _writter.WriteLine(Environment.NewLine); var range = ipHelper.GetIPRange(_startAddress, _endAddress); //RunScan(range, ipHelper); RunScanParallel(range, ipHelper); _writter.WriteLine(Environment.NewLine); _writter.WriteLine($"Exiting application {DateTime.Now:yyyy.MM.dd HH:mm:sss}"); }