Add linux support and code improvments

This commit is contained in:
Stone_Red
2023-05-03 22:15:14 +02:00
parent 9a9d858cfd
commit 6df8862dbc
3 changed files with 21 additions and 50 deletions
+2 -1
View File
@@ -9,7 +9,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="IPAddressRange" Version="4.2.0" />
<PackageReference Include="ArpLookup" Version="2.0.3" />
<PackageReference Include="IPAddressRange" Version="5.0.0" />
<PackageReference Include="Stone_Red-C-Sharp-Utilities" Version="1.0.3.1" />
</ItemGroup>
-39
View File
@@ -1,39 +0,0 @@
using Stone_Red_Utilities.ConsoleExtentions;
using System.Net;
using System.Runtime.InteropServices;
namespace ARP_Scanner;
internal class ArpUtilities
{
[DllImport("iphlpapi.dll", ExactSpelling = true)]
private static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr, ref uint PhyAddrLen);
private uint macAddrLen = (uint)new byte[6].Length;
public string? SendArpRequest(IPAddress ipAddress)
{
byte[] macAddr = new byte[6];
try
{
_ = SendARP(BitConverter.ToInt32(ipAddress.GetAddressBytes(), 0), 0, macAddr, ref macAddrLen);
if (MacAddresstoString(macAddr) != "00-00-00-00-00-00")
{
return MacAddresstoString(macAddr);
}
}
catch (Exception e)
{
ConsoleExt.WriteLine(e.Message, ConsoleColor.Red);
}
return null;
}
public static string MacAddresstoString(byte[] macAdrr)
{
string macString = BitConverter.ToString(macAdrr);
return macString.ToUpper();
}
}
+19 -10
View File
@@ -1,4 +1,4 @@
using ARP_Scanner;
using ArpLookup;
using NetTools;
@@ -7,15 +7,24 @@ using Stone_Red_Utilities.ConsoleExtentions;
using System.Collections.Concurrent;
using System.Net;
using System.Net.NetworkInformation;
internal class Program
namespace ARP_Scanner;
internal static class Program
{
private static void Main(string[] args)
private static async Task Main(string[] args)
{
bool success = false;
IPAddress[]? ipAddresses = null;
MacVendorLookup macVendorLookup = new MacVendorLookup("mac-vendors.csv");
if (!Arp.IsSupported)
{
ConsoleExt.WriteLine("ARP is not supported on this platform!", ConsoleColor.Red);
return;
}
if (args.Length >= 1)
{
success = IPAddressRange.TryParse(string.Join("", args), out IPAddressRange iPAddressRange);
@@ -38,21 +47,21 @@ internal class Program
ConsoleExt.WriteLine("Starting scan...", ConsoleColor.DarkYellow);
Parallel.ForEach(ipAddresses, ipAddress =>
await Parallel.ForEachAsync(ipAddresses, async (ipAddress, _) =>
{
string? mac = new ArpUtilities().SendArpRequest(ipAddress);
PhysicalAddress? mac = await Arp.LookupAsync(ipAddress);
Interlocked.Increment(ref processedIpAddressesCount);
int localProcessedIpAddressesCount = Interlocked.Increment(ref processedIpAddressesCount);
if (mac is not null)
{
List<string> info = new List<string> { ipAddress.ToString(), mac };
info.AddRange(macVendorLookup.GetInformation(mac));
ConsoleExt.WriteLine($"Progress: {processedIpAddressesCount}/{ipAddressesCount} [{100d / ipAddressesCount * processedIpAddressesCount:0.00}%] | Active: {ipAddress}", ConsoleColor.Green);
List<string> info = new List<string> { ipAddress.ToString(), mac.ToString() };
info.AddRange(macVendorLookup.GetInformation(mac.ToString()));
ConsoleExt.WriteLine($"Progress: {localProcessedIpAddressesCount}/{ipAddressesCount} [{100d / ipAddressesCount * localProcessedIpAddressesCount:0.00}%] | Active: {ipAddress}", ConsoleColor.Green);
activeHosts.Add(info.ToArray());
}
else
{
ConsoleExt.WriteLine($"Progress: {processedIpAddressesCount}/{ipAddressesCount} [{100d / ipAddressesCount * processedIpAddressesCount:0.00}%] | Inactive: {ipAddress}", ConsoleColor.Red);
ConsoleExt.WriteLine($"Progress: {localProcessedIpAddressesCount}/{ipAddressesCount} [{100d / ipAddressesCount * localProcessedIpAddressesCount:0.00}%] | Inactive: {ipAddress}", ConsoleColor.Red);
}
});