diff --git a/networkdiscovery2.ps1 b/networkdiscovery2.ps1 index ca05fb6..c4d5d8f 100644 --- a/networkdiscovery2.ps1 +++ b/networkdiscovery2.ps1 @@ -183,24 +183,71 @@ function Get-DeviceCategory { } } +# Function to get MAC vendor from MAC address using our database +function Get-MacVendor { + param ( + [string]$MacAddress + ) + + # Clean the MAC address and get first 6 characters + $oui = ($MacAddress -replace '[-:\.]', '').Substring(0, 6).ToUpper() + + # Look up the manufacturer in our database + if ($ouiDatabase.ContainsKey($oui)) { + return $ouiDatabase[$oui] + } + return "Unknown" +} + +# Function to categorize devices (kept same as previous version) +function Get-DeviceCategory { + param ( + [string]$Manufacturer, + [string]$DeviceName + ) + # ... (previous categorization logic) ... +} + # Get local subnet information $localIP = (Get-NetIPAddress | Where-Object {$_.AddressFamily -eq 'IPv4' -and $_.PrefixOrigin -eq 'Dhcp'}).IPAddress -$subnet = $localIP -replace '\.\d+$', '.0/24' +$subnet = $localIP -replace '\.\d+$', '' + +# Fast parallel ping sweep to populate ARP cache +Write-Host "Performing rapid ping sweep..." -ForegroundColor Yellow +$pingJobs = 1..254 | ForEach-Object { + $ip = "$subnet.$_" + Start-Job -ScriptBlock { + param($ip) + Test-Connection -ComputerName $ip -Count 1 -ErrorAction SilentlyContinue + } -ArgumentList $ip +} + +# Wait for all pings to complete +Wait-Job $pingJobs | Out-Null +Remove-Job $pingJobs + +# Short delay to ensure ARP cache is updated +Start-Sleep -Seconds 2 + +# Get ARP cache +Write-Host "Retrieving network information..." -ForegroundColor Yellow +$arpCache = Get-NetNeighbor | Where-Object { + $_.State -ne 'Unreachable' -and + $_.LinkLayerAddress -ne '000000000000' -and + $_.LinkLayerAddress -ne $null +} # Initialize results array $deviceList = @() -# Ping sweep the network -1..254 | ForEach-Object { - $ip = $subnet -replace '0/24', $_ - $ping = Test-Connection -ComputerName $ip -Count 1 -ErrorAction SilentlyContinue - - if ($ping) { - # Get ARP entry for the IP - $arpEntry = Get-NetNeighbor -IPAddress $ip -ErrorAction SilentlyContinue +# Process each valid ARP entry +foreach ($entry in $arpCache) { + if ($entry.LinkLayerAddress) { + $ip = $entry.IPAddress + $macAddress = $entry.LinkLayerAddress - if ($arpEntry -and $arpEntry.LinkLayerAddress -and $arpEntry.LinkLayerAddress -ne '000000000000') { - $macAddress = $arpEntry.LinkLayerAddress + # Only process if MAC address is valid (not zeros) + if ($macAddress -ne '000000000000') { $manufacturer = Get-MacVendor -MacAddress $macAddress $deviceName = try { [System.Net.Dns]::GetHostEntry($ip).HostName @@ -208,7 +255,6 @@ $deviceList = @() "Unknown" } - # Only add devices with valid MAC addresses and known manufacturers $deviceList += [PSCustomObject]@{ IPAddress = $ip DeviceName = $deviceName