Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions NetDiagram-PS/NetDiagram-PS.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,38 @@ function Get-LocalARPTable {
}
}

function Wait-DnsLookupTask {
param(
[Parameter(Mandatory)][System.Threading.Tasks.Task]$LookupTask,
[Parameter(Mandatory)][string]$IPAddress,
[Parameter(Mandatory)][int]$TimeoutSeconds
)

try {
if ($LookupTask.Wait([TimeSpan]::FromSeconds($TimeoutSeconds))) {
$result = $LookupTask.Result
return [pscustomobject]@{
IPAddress = $IPAddress
Hostname = $result.HostName
Aliases = $result.Aliases
Success = $true
}
}

Write-Verbose "DNS lookup for $IPAddress timed out after $TimeoutSeconds second(s)"
}
catch {
Write-Verbose "DNS lookup for $IPAddress failed: $($_.Exception.Message)"
}

return [pscustomobject]@{
IPAddress = $IPAddress
Hostname = $null
Aliases = @()
Success = $false
}
}

function Resolve-IPHostname {
<#
.SYNOPSIS
Expand Down Expand Up @@ -1406,25 +1438,7 @@ function Resolve-IPHostname {
# pipeline. On timeout we return a failure object (the background task
# is abandoned).
$task = [System.Net.Dns]::GetHostEntryAsync($ip)

if ($task.Wait([TimeSpan]::FromSeconds($TimeoutSeconds))) {
$result = $task.Result
[pscustomobject]@{
IPAddress = $ip
Hostname = $result.HostName
Aliases = $result.Aliases
Success = $true
}
}
else {
Write-Verbose "DNS lookup for $ip timed out after $TimeoutSeconds second(s)"
[pscustomobject]@{
IPAddress = $ip
Hostname = $null
Aliases = @()
Success = $false
}
}
Wait-DnsLookupTask -LookupTask $task -IPAddress $ip -TimeoutSeconds $TimeoutSeconds
}
catch {
[pscustomobject]@{
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ pwsh .\examples\New-NetworkDiagram.ps1 -ScanDepth Full -OutputPath .\office-netw

# Choose an interface explicitly (useful with VPNs or multiple adapters)
pwsh .\examples\New-NetworkDiagram.ps1 -InterfaceName en0

# Limit each reverse DNS lookup to one second
pwsh .\examples\New-NetworkDiagram.ps1 -DnsTimeoutSeconds 1
```

### Method 2: Manual Workflow (manual inventory)
Expand Down
19 changes: 16 additions & 3 deletions examples/New-NetworkDiagram.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
.PARAMETER InterfaceName
Interface name or numeric interface index to scan. By default, the interface
used by the active IPv4 default route is selected.
.PARAMETER DnsTimeoutSeconds
Maximum time for each reverse DNS lookup during discovery (default: 2 seconds).
.EXAMPLE
.\New-NetworkDiagram.ps1

Expand All @@ -33,7 +35,10 @@ param(

[string]$OutputPath = '.\my-network.drawio',

[string]$InterfaceName
[string]$InterfaceName,

[ValidateRange(1, 30)]
[int]$DnsTimeoutSeconds = 2
)

function Get-WizardInventoryPath {
Expand Down Expand Up @@ -521,8 +526,16 @@ $scanTargets | ForEach-Object -Parallel {

if ($result) {
$hostname = try {
$resolved = [System.Net.Dns]::GetHostEntry($testIP)
$resolved.HostName
if (-not (Get-Command Resolve-IPHostname -ErrorAction SilentlyContinue)) {
Import-Module $using:modulePath -Force
}
$resolved = Resolve-IPHostname -IPAddress $testIP -TimeoutSeconds $using:DnsTimeoutSeconds
if ($resolved.Success -and -not [string]::IsNullOrWhiteSpace($resolved.Hostname)) {
$resolved.Hostname
}
else {
"Device-$testIP"
}
} catch {
"Device-$testIP"
}
Expand Down
32 changes: 32 additions & 0 deletions tests/NetDiagram.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,38 @@ Describe 'Resolve-IPHostname' {
$result.IPAddress | Should -Be '192.0.2.1'
$result.Success | Should -Be $false
}

It 'Returns within the configured deadline for a stalled resolver task' {
InModuleScope 'NetDiagram-PS' {
$source = [System.Threading.Tasks.TaskCompletionSource[System.Net.IPHostEntry]]::new()
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()

$result = Wait-DnsLookupTask -LookupTask $source.Task `
-IPAddress '192.0.2.10' -TimeoutSeconds 1
$stopwatch.Stop()

$result.IPAddress | Should -Be '192.0.2.10'
$result.Hostname | Should -BeNullOrEmpty
$result.Success | Should -Be $false
$stopwatch.Elapsed.TotalSeconds | Should -BeLessThan 2.5
}
}

It 'Returns a failure immediately for a faulted resolver task' {
InModuleScope 'NetDiagram-PS' {
$source = [System.Threading.Tasks.TaskCompletionSource[System.Net.IPHostEntry]]::new()
$source.SetException([InvalidOperationException]::new('fixture failure'))
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()

$result = Wait-DnsLookupTask -LookupTask $source.Task `
-IPAddress '192.0.2.11' -TimeoutSeconds 5
$stopwatch.Stop()

$result.IPAddress | Should -Be '192.0.2.11'
$result.Success | Should -Be $false
$stopwatch.Elapsed.TotalSeconds | Should -BeLessThan 1
}
}
}

Describe 'Invoke-SnmpWalk' {
Expand Down