/// <summary> /// Synchronously scan a subsequent range of IP addresses for information. /// </summary> /// <param name="ip1">The beginning endpoint that intializes the scan.</param> /// <param name="ip2">The ending endpoint that the scan will stop after.</param> public List <IPScanObject> ScanRange(string ip1, string ip2) { // Keep track of our scan progress. int progress = 0; // Create our list of scan objects to return. List <IPScanObject> results = new List <IPScanObject>(); try { // Parse our IPs into an IPAddressRange object. IPAddress address1 = IPAddress.Parse(ip1); IPAddress address2 = IPAddress.Parse(ip2); IPAddressRange range = new IPAddressRange(address1, address2); // Loop through our range and begin scanning each IP. int total = 0; foreach (IPAddress x in range) { total++; } // Grab number of addresses before proceeding. int count = 0; // Amount of addresses scanned. foreach (IPAddress address in range) { // Iterate up so we can report progress to the user. count++; // Let the user know which address we are currently scanning. UpdateScanRangeProgress(progress, address.ToString()); UpdateScanRangeAsyncProgress(progress, address.ToString()); // Scan our current address. IPScanObject result = Scan(address.ToString()); results.Add(result); // Calculate our progress percentage. int x = (int)(((double)count / (double)total) * 100); if (x > progress) { progress = x; UpdateScanRangeProgress(progress, address.ToString()); UpdateScanRangeAsyncProgress(progress, address.ToString()); } } } catch (Exception ex) { // Create a new scan object containing our errors instead of returning null. IPScanObject result = new IPScanObject(); result.Errors = ex; results.Add(result); return(results); } // Update our completed event. UpdateScanRangeResults(results); UpdateScanRangeAsyncResults(results); // Return our list of scanned IP addresses. return(results); }
/// <summary> /// Synchronously scan a pre-formatted list of IP addresses for information. /// </summary> /// <param name="list">A list of IPs formatted as such: "address1, address2, address3, ..."</param> public List <IPScanObject> ScanList(string list) { // Track our progress. int progress = 0; // Create a list of scan objects to return. List <IPScanObject> results = new List <IPScanObject>(); try { // Trim all whitespace from our list of addresses. string trimmed = Regex.Replace(list, @"\s+", ""); // Split our list into an array so we can access each address individually. string[] addresses = trimmed.Split(','); // Loop through our addresses and begin scanning each one. int count = 0; foreach (string address in addresses) { // Iterate up so we can report progress to the user. count++; // Let the user know which address we are currently scanning. UpdateScanListProgress(progress, address); UpdateScanListAsyncProgress(progress, address); // Scan our current address. IPScanObject result = Scan(address); results.Add(result); // Update our progress percentage. int x = (int)(((double)count / (double)addresses.Length) * 100); if (x > progress) { progress = x; UpdateScanListProgress(progress, address); UpdateScanListAsyncProgress(progress, address); } } } catch (Exception ex) // Catch if we are unable to parse the list. { // Create a new scan object containing our errors instead of returning null. IPScanObject result = new IPScanObject(); result.Errors = ex; results.Add(result); return(results); } // Update our event with the results. UpdateScanListResults(results); UpdateScanListAsyncResults(results); // Return our list of scanned IP addresses. return(results); }
private void UpdateScanAsyncResults(IPScanObject result) { if (ScanAsyncComplete == null) { return; } ScanAsyncCompleteEventArgs args = new ScanAsyncCompleteEventArgs(result); ScanAsyncComplete(this, args); }
public ScanAsyncCompleteEventArgs(IPScanObject result) { Result = result; }
/// <summary> /// Synchronously scan a single local IP address in order to obtain more information about it. /// </summary> /// <param name="ip">The endpoint to enumerate information from.</param> /// <returns></returns> public IPScanObject Scan(string ip) { // Create a stopwatch to track elapsed time. Stopwatch time = Stopwatch.StartNew(); // Create a blank scan object for our return. IPScanObject result = new IPScanObject(ip, 0, "n/a", "n/a", null, false); try { // Create object shells and try to fill them with data. long ping = 0; string hostname = "n/a"; string mac = "n/a"; PKScanObject ports = new PKScanObject(); bool online = false; //Ping our IP. if (Fetchers.Ping) { ping = GetAveragePingResponse(ip, Probes, Timeout); } UpdateScanProgress(10); UpdateScanAsyncProgress(10); // Get our hostname. if (Fetchers.Hostname) { IPHostEntry entry = Dns.GetHostEntry(ip); if (entry != null) { hostname = entry.HostName; } } UpdateScanProgress(35); UpdateScanAsyncProgress(35); // Get our MAC address. if (Fetchers.MAC) { ArpRequestResult request = ArpRequest.Send(IPAddress.Parse(ip)); if (request.Exception != null) { mac = "n/a"; } else { // Format our address before passing it. int start = 0; string output = null; while (start < request.Address.ToString().Length) { output += request.Address.ToString().Substring(start, Math.Min(2, request.Address.ToString().Length - start)) + ":"; start += 2; } mac = output.Remove(output.Length - 1, 1); } } UpdateScanProgress(58); UpdateScanAsyncProgress(58); // Port knock all ports. // Let the ports be null for now since it's CPU intensive. UpdateScanProgress(75); UpdateScanAsyncProgress(75); // Set our online flag. if (Fetchers.Online) { if (IsHostAlive(ip, Probes, Timeout)) { online = true; } } UpdateScanProgress(100); UpdateScanAsyncProgress(100); // Create a new scan object with our results. time.Stop(); result = new IPScanObject(ip, ping, hostname, mac, ports, online); } catch (Exception ex) { result.Errors = ex; } // Let it return a blank object containing errors. // Return our scanned object even if it's an error shell. result.Elapsed = time.Elapsed.TotalSeconds; UpdateScanResults(result); UpdateScanAsyncResults(result); return(result); }