Updated version. OUI still not working well.

main
cole@cybertek.systems 2025-01-06 21:58:23 -06:00
parent eacff16c89
commit 7c49f0b8f5
1 changed files with 58 additions and 12 deletions

View File

@ -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 # Get local subnet information
$localIP = (Get-NetIPAddress | Where-Object {$_.AddressFamily -eq 'IPv4' -and $_.PrefixOrigin -eq 'Dhcp'}).IPAddress $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 # Initialize results array
$deviceList = @() $deviceList = @()
# Ping sweep the network # Process each valid ARP entry
1..254 | ForEach-Object { foreach ($entry in $arpCache) {
$ip = $subnet -replace '0/24', $_ if ($entry.LinkLayerAddress) {
$ping = Test-Connection -ComputerName $ip -Count 1 -ErrorAction SilentlyContinue $ip = $entry.IPAddress
$macAddress = $entry.LinkLayerAddress
if ($ping) { # Only process if MAC address is valid (not zeros)
# Get ARP entry for the IP if ($macAddress -ne '000000000000') {
$arpEntry = Get-NetNeighbor -IPAddress $ip -ErrorAction SilentlyContinue
if ($arpEntry -and $arpEntry.LinkLayerAddress -and $arpEntry.LinkLayerAddress -ne '000000000000') {
$macAddress = $arpEntry.LinkLayerAddress
$manufacturer = Get-MacVendor -MacAddress $macAddress $manufacturer = Get-MacVendor -MacAddress $macAddress
$deviceName = try { $deviceName = try {
[System.Net.Dns]::GetHostEntry($ip).HostName [System.Net.Dns]::GetHostEntry($ip).HostName
@ -208,7 +255,6 @@ $deviceList = @()
"Unknown" "Unknown"
} }
# Only add devices with valid MAC addresses and known manufacturers
$deviceList += [PSCustomObject]@{ $deviceList += [PSCustomObject]@{
IPAddress = $ip IPAddress = $ip
DeviceName = $deviceName DeviceName = $deviceName