diff --git a/NetDiagram-PS/NetDiagram-PS.psm1 b/NetDiagram-PS/NetDiagram-PS.psm1 index e84ea15..5034ed6 100644 --- a/NetDiagram-PS/NetDiagram-PS.psm1 +++ b/NetDiagram-PS/NetDiagram-PS.psm1 @@ -1251,6 +1251,53 @@ function Compare-NetworkScans { #region Level 1: Credential-Free Discovery Functions +function ConvertFrom-ArpText { + param( + [Parameter(Mandatory)][string[]]$Lines, + [Parameter(Mandatory)][ValidateSet('MacOS', 'LinuxIp', 'LinuxArp')][string]$Format, + [string]$InterfaceAlias + ) + + foreach ($line in $Lines) { + $entry = $null + if ($Format -eq 'MacOS') { + if ($line -notmatch '\s+at\s+') { continue } + if ($line -match '\s+at\s+\(incomplete\)') { continue } + if ($line -notmatch '\((\d+\.\d+\.\d+\.\d+)\)\s+at\s+([0-9a-fA-F:]{17})\s+on\s+(\S+)') { + throw "Unable to parse macOS ARP entry: $line" + } + $entry = [pscustomobject]@{ + IPAddress = $matches[1]; MACAddress = $matches[2].ToUpperInvariant() + State = 'Reachable'; InterfaceAlias = $matches[3]; InterfaceIndex = $null + } + } + elseif ($Format -eq 'LinuxIp') { + if ($line -notmatch '\s+lladdr\s+') { continue } + if ($line -notmatch '^(\d+\.\d+\.\d+\.\d+)\s+dev\s+(\S+)\s+lladdr\s+([0-9a-fA-F:]{17})\s+(\S+)') { + throw "Unable to parse Linux ip-neigh entry: $line" + } + $entry = [pscustomobject]@{ + IPAddress = $matches[1]; MACAddress = $matches[3].ToUpperInvariant() + State = $matches[4]; InterfaceAlias = $matches[2]; InterfaceIndex = $null + } + } + else { + if ($line -notmatch '\s+at\s+') { continue } + if ($line -notmatch '\((\d+\.\d+\.\d+\.\d+)\)\s+at\s+([0-9a-fA-F:]{17})') { + throw "Unable to parse Linux arp entry: $line" + } + $entry = [pscustomobject]@{ + IPAddress = $matches[1]; MACAddress = $matches[2].ToUpperInvariant() + State = 'Reachable'; InterfaceAlias = $null; InterfaceIndex = $null + } + } + + if (-not $InterfaceAlias -or $entry.InterfaceAlias -eq $InterfaceAlias) { + $entry + } + } +} + function Get-LocalARPTable { <# .SYNOPSIS @@ -1258,6 +1305,8 @@ function Get-LocalARPTable { .DESCRIPTION Parses the ARP cache to find MAC addresses and IP addresses of devices that have recently communicated with this host. No credentials required. + Missing platform commands, command failures, and recognized-but-malformed + neighbor lines produce terminating errors instead of an empty-cache result. .PARAMETER InterfaceAlias Optional network interface to filter results .EXAMPLE @@ -1275,12 +1324,11 @@ function Get-LocalARPTable { [string]$InterfaceAlias ) - try { - $arpEntries = @() - - if ($IsWindows -or $PSVersionTable.PSVersion.Major -lt 6 -or $null -eq $IsWindows) { + $arpEntries = @() + + if ($IsWindows) { # Windows: Use Get-NetNeighbor - $neighbors = Get-NetNeighbor -AddressFamily IPv4 -ErrorAction SilentlyContinue + $neighbors = Get-NetNeighbor -AddressFamily IPv4 -ErrorAction Stop if ($InterfaceAlias) { $neighbors = $neighbors | Where-Object { $_.InterfaceAlias -eq $InterfaceAlias } @@ -1295,75 +1343,36 @@ function Get-LocalARPTable { InterfaceIndex = $entry.InterfaceIndex } } - } elseif ($IsMacOS) { - # macOS: Parse arp -an + } + elseif ($IsMacOS) { + $null = Get-Command arp -CommandType Application -ErrorAction Stop $arpOutput = & arp -an 2>&1 - foreach ($line in $arpOutput) { - # Format: ? (192.168.1.1) at aa:bb:cc:dd:ee:ff on en0 ifscope [ethernet] - if ($line -match '\((\d+\.\d+\.\d+\.\d+)\)\s+at\s+([0-9a-f:]+)\s+on\s+(\w+)') { - $ip = $matches[1] - $mac = $matches[2] - $interface = $matches[3] - - if (-not $InterfaceAlias -or $interface -eq $InterfaceAlias) { - $arpEntries += [pscustomobject]@{ - IPAddress = $ip - MACAddress = $mac - State = 'Reachable' - InterfaceAlias = $interface - InterfaceIndex = $null - } - } - } + if ($LASTEXITCODE -ne 0) { + throw "arp -an failed with exit code $LASTEXITCODE" } - } elseif ($IsLinux) { - # Linux: Parse ip neigh or arp - $neighborOutput = & ip neigh show 2>&1 - if ($LASTEXITCODE -eq 0) { - # Use ip neigh - foreach ($line in $neighborOutput) { - # Format: 192.168.1.1 dev eth0 lladdr aa:bb:cc:dd:ee:ff REACHABLE - if ($line -match '(\d+\.\d+\.\d+\.\d+)\s+dev\s+(\w+)\s+lladdr\s+([0-9a-f:]+)\s+(\w+)') { - $ip = $matches[1] - $interface = $matches[2] - $mac = $matches[3] - $state = $matches[4] - - if (-not $InterfaceAlias -or $interface -eq $InterfaceAlias) { - $arpEntries += [pscustomobject]@{ - IPAddress = $ip - MACAddress = $mac - State = $state - InterfaceAlias = $interface - InterfaceIndex = $null - } - } - } + $arpEntries = @(ConvertFrom-ArpText -Lines $arpOutput -Format MacOS -InterfaceAlias $InterfaceAlias) + } + elseif ($IsLinux) { + $ipCommand = Get-Command ip -CommandType Application -ErrorAction SilentlyContinue + if ($ipCommand) { + $neighborOutput = & $ipCommand.Source neigh show 2>&1 + if ($LASTEXITCODE -ne 0) { + throw "ip neigh show failed with exit code $LASTEXITCODE" } - } else { - # Fallback to arp command - $arpOutput = & arp -an 2>&1 - foreach ($line in $arpOutput) { - if ($line -match '\((\d+\.\d+\.\d+\.\d+)\)\s+at\s+([0-9a-f:]+)') { - $arpEntries += [pscustomobject]@{ - IPAddress = $matches[1] - MACAddress = $matches[2] - State = 'Reachable' - InterfaceAlias = $null - InterfaceIndex = $null - } - } + $arpEntries = @(ConvertFrom-ArpText -Lines $neighborOutput -Format LinuxIp -InterfaceAlias $InterfaceAlias) + } + else { + $arpCommand = Get-Command arp -CommandType Application -ErrorAction Stop + $arpOutput = & $arpCommand.Source -an 2>&1 + if ($LASTEXITCODE -ne 0) { + throw "arp -an failed with exit code $LASTEXITCODE" } + $arpEntries = @(ConvertFrom-ArpText -Lines $arpOutput -Format LinuxArp -InterfaceAlias $InterfaceAlias) } - } - - Write-Verbose "Found $($arpEntries.Count) ARP entries" - return $arpEntries - } - catch { - Write-Warning "Failed to retrieve ARP table: $_" - return @() } + + Write-Verbose "Found $($arpEntries.Count) ARP entries" + return $arpEntries } function Resolve-IPHostname { diff --git a/tests/NetDiagram.Tests.ps1 b/tests/NetDiagram.Tests.ps1 index e321402..080c99f 100644 --- a/tests/NetDiagram.Tests.ps1 +++ b/tests/NetDiagram.Tests.ps1 @@ -389,6 +389,117 @@ Describe 'Defensive Coding Tests' { } } +Describe 'Get-LocalARPTable parsing (#13 regression)' { + It 'Parses macOS arp output and filters by interface' { + InModuleScope 'NetDiagram-PS' { + $lines = @( + '? (192.168.1.1) at aa:bb:cc:dd:ee:ff on en0 ifscope [ethernet]' + '? (192.168.1.2) at 11:22:33:44:55:66 on bridge0 ifscope [ethernet]' + '? (192.168.1.3) at (incomplete) on en0 ifscope [ethernet]' + ) + + $result = @(ConvertFrom-ArpText -Lines $lines -Format MacOS -InterfaceAlias 'en0') + $result | Should -HaveCount 1 + $result[0].IPAddress | Should -Be '192.168.1.1' + $result[0].MACAddress | Should -Be 'AA:BB:CC:DD:EE:FF' + $result[0].InterfaceAlias | Should -Be 'en0' + } + } + + It 'Parses Linux ip-neigh output including dotted interface names' { + InModuleScope 'NetDiagram-PS' { + $lines = @( + '192.168.1.1 dev eth0.20 lladdr aa:bb:cc:dd:ee:ff REACHABLE' + '192.168.1.2 dev eth0 lladdr 11:22:33:44:55:66 STALE' + '192.168.1.3 dev eth0 FAILED' + ) + + $result = @(ConvertFrom-ArpText -Lines $lines -Format LinuxIp -InterfaceAlias 'eth0.20') + $result | Should -HaveCount 1 + $result[0].IPAddress | Should -Be '192.168.1.1' + $result[0].State | Should -Be 'REACHABLE' + $result[0].InterfaceAlias | Should -Be 'eth0.20' + } + } + + It 'Parses Linux arp fallback output' { + InModuleScope 'NetDiagram-PS' { + $lines = @('? (10.0.0.1) at de:ad:be:ef:00:01 [ether] on eth0') + $result = @(ConvertFrom-ArpText -Lines $lines -Format LinuxArp) + + $result | Should -HaveCount 1 + $result[0].IPAddress | Should -Be '10.0.0.1' + $result[0].MACAddress | Should -Be 'DE:AD:BE:EF:00:01' + } + } + + It 'Surfaces a candidate line that no longer matches the expected format' { + InModuleScope 'NetDiagram-PS' { + { ConvertFrom-ArpText -Lines @('192.168.1.1 dev eth0 lladdr malformed REACHABLE') -Format LinuxIp } | + Should -Throw '*Unable to parse Linux ip-neigh entry*' + } + } +} + +Describe 'Invoke-PortScan loopback behavior (#13 regression)' { + It 'Reports an open port, omits a closed port, and captures a banner' { + $portProbe = [System.Net.Sockets.TcpListener]::new([System.Net.IPAddress]::Loopback, 0) + $portProbe.Start() + $openPort = ([System.Net.IPEndPoint]$portProbe.LocalEndpoint).Port + $portProbe.Stop() + + do { + $closedProbe = [System.Net.Sockets.TcpListener]::new([System.Net.IPAddress]::Loopback, 0) + $closedProbe.Start() + $closedPort = ([System.Net.IPEndPoint]$closedProbe.LocalEndpoint).Port + $closedProbe.Stop() + } while ($closedPort -eq $openPort) + + $serverJob = Start-ThreadJob -ArgumentList $openPort -ScriptBlock { + param($Port) + $listener = [System.Net.Sockets.TcpListener]::new([System.Net.IPAddress]::Loopback, $Port) + try { + $listener.Start() + 'READY' + $client = $listener.AcceptTcpClient() + try { + $stream = $client.GetStream() + $bytes = [System.Text.Encoding]::ASCII.GetBytes('PSNETMAP-TEST') + $stream.Write($bytes, 0, $bytes.Length) + $stream.Flush() + } + finally { + $client.Dispose() + } + } + finally { + $listener.Stop() + } + } + + try { + $deadline = [DateTime]::UtcNow.AddSeconds(5) + do { + $ready = @(Receive-Job -Job $serverJob -Keep) -contains 'READY' + if (-not $ready) { Start-Sleep -Milliseconds 25 } + } while (-not $ready -and [DateTime]::UtcNow -lt $deadline) + $ready | Should -Be $true + + $result = Invoke-PortScan -IPAddress '127.0.0.1' ` + -Ports @($openPort, $closedPort) -TimeoutMs 1000 -GrabBanners + + $result.OpenPorts.Port | Should -Contain $openPort + $result.OpenPorts.Port | Should -Not -Contain $closedPort + ($result.OpenPorts | Where-Object Port -eq $openPort).Banner | + Should -Be 'PSNETMAP-TEST' + } + finally { + Stop-Job -Job $serverJob -ErrorAction SilentlyContinue + Remove-Job -Job $serverJob -Force -ErrorAction SilentlyContinue + } + } +} + Describe 'Get-MACVendor' { It 'Should resolve a known OUI to its vendor' { $result = Get-MACVendor -MACAddress '00:1A:A0:12:34:56'