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
2,665 changes: 11 additions & 2,654 deletions NetDiagram-PS/NetDiagram-PS.psm1

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions NetDiagram-PS/Private/ConvertFrom-ArpText.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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
}
}
}
6 changes: 6 additions & 0 deletions NetDiagram-PS/Private/ConvertFrom-UInt32Address.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function ConvertFrom-UInt32Address {
param([Parameter(Mandatory)][uint32]$Value)
$bytes = [System.BitConverter]::GetBytes($Value)
if ([System.BitConverter]::IsLittleEndian) { [Array]::Reverse($bytes) }
return ([System.Net.IPAddress]::new($bytes)).ToString()
}
13 changes: 13 additions & 0 deletions NetDiagram-PS/Private/ConvertTo-NormalizedIPv4Address.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function ConvertTo-NormalizedIPv4Address {
param(
[Parameter(Mandatory)][string]$Value,
[Parameter(Mandatory)][string]$Context
)

$address = $null
if (-not [System.Net.IPAddress]::TryParse($Value, [ref]$address) -or
$address.AddressFamily -ne [System.Net.Sockets.AddressFamily]::InterNetwork) {
throw "Invalid inventory $Context '$Value': expected an IPv4 address. IPv6 is not supported."
}
return $address.ToString()
}
34 changes: 34 additions & 0 deletions NetDiagram-PS/Private/ConvertTo-NormalizedIPv4Cidr.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function ConvertTo-NormalizedIPv4Cidr {
param(
[Parameter(Mandatory)][string]$Value,
[Parameter(Mandatory)][string]$Context
)

if ($Value -notmatch '^(.+)\/(\d{1,2})$') {
throw "Invalid inventory $Context '$Value': expected IPv4 CIDR notation."
}

$address = ConvertTo-NormalizedIPv4Address -Value $matches[1] -Context $Context
$prefixLength = [int]$matches[2]
if ($prefixLength -gt 32) {
throw "Invalid inventory $Context '$Value': IPv4 prefix must be between 0 and 32."
}

$bytes = ([System.Net.IPAddress]::Parse($address)).GetAddressBytes()
$bitsRemaining = $prefixLength
for ($index = 0; $index -lt $bytes.Count; $index++) {
$mask = if ($bitsRemaining -ge 8) {
255
}
elseif ($bitsRemaining -le 0) {
0
}
else {
256 - [Math]::Pow(2, 8 - $bitsRemaining)
}
$bytes[$index] = [byte]($bytes[$index] -band [int]$mask)
$bitsRemaining -= 8
}

return "$([System.Net.IPAddress]::new($bytes))/$prefixLength"
}
6 changes: 6 additions & 0 deletions NetDiagram-PS/Private/ConvertTo-UInt32Address.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function ConvertTo-UInt32Address {
param([Parameter(Mandatory)][string]$IPAddress)
$bytes = [System.Net.IPAddress]::Parse($IPAddress).GetAddressBytes()
if ([System.BitConverter]::IsLittleEndian) { [Array]::Reverse($bytes) }
return [System.BitConverter]::ToUInt32($bytes, 0)
}
16 changes: 16 additions & 0 deletions NetDiagram-PS/Private/Get-LayerFromRole.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function Get-LayerFromRole {
<#
.SYNOPSIS
Maps device role to network layer for diagram placement
#>
param([string]$Role)

switch ($Role) {
'core-router' { return 'Core' }
'router' { return 'Core' }
'distribution'{ return 'Dist' }
'switch' { return 'Access' }
'server' { return 'Servers' }
default { return 'Access' }
}
}
6 changes: 6 additions & 0 deletions NetDiagram-PS/Private/Get-PrefixMask.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function Get-PrefixMask {
param([Parameter(Mandatory)][int]$PrefixLength)
if ($PrefixLength -le 0) { return [uint32]0 }
if ($PrefixLength -ge 32) { return [uint32]4294967295 }
return [uint32]((([uint64]4294967295) -shl (32 - $PrefixLength)) -band [uint64]4294967295)
}
29 changes: 29 additions & 0 deletions NetDiagram-PS/Private/Get-ServiceName.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function Get-ServiceName {
<#
.SYNOPSIS
Maps port numbers to common service names
#>
param([int]$Port)

$services = @{
21 = 'FTP'
22 = 'SSH'
23 = 'Telnet'
25 = 'SMTP'
53 = 'DNS'
80 = 'HTTP'
110 = 'POP3'
143 = 'IMAP'
161 = 'SNMP'
443 = 'HTTPS'
445 = 'SMB'
3389 = 'RDP'
8080 = 'HTTP-ALT'
8443 = 'HTTPS-ALT'
}

if ($services.ContainsKey($Port)) {
return $services[$Port]
}
return "Unknown"
}
37 changes: 37 additions & 0 deletions NetDiagram-PS/Private/Get-SubnetScanTarget.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function Get-SubnetScanTarget {
<#
.SYNOPSIS
Returns the list of host addresses (as UInt32 values) to scan within a subnet,
derived from the real network/broadcast bounds and bounded by scan depth.
#>
param(
[Parameter(Mandatory)][uint32]$NetworkValue,
[Parameter(Mandatory)][uint32]$BroadcastValue,
[ValidateSet('Quick', 'Medium', 'Full')][string]$ScanDepth = 'Quick',
[int]$MaxScanHosts = 1022
)

[int64]$firstHost = [int64]$NetworkValue + 1
[int64]$lastHost = [int64]$BroadcastValue - 1
[int64]$usableCount = if ($lastHost -ge $firstHost) { $lastHost - $firstHost + 1 } else { 0 }

$hostValues = [System.Collections.Generic.List[uint32]]::new()
if ($usableCount -le 0) {
}
elseif ($ScanDepth -eq 'Quick') {
$sampleCount = [int][Math]::Min(8, $usableCount)
for ($s = 0; $s -lt $sampleCount; $s++) {
$idx = [int][Math]::Floor($s * $usableCount / $sampleCount)
$null = $hostValues.Add([uint32]($firstHost + $idx))
}
}
elseif ($ScanDepth -eq 'Medium') {
$take = [int][Math]::Min(50, $usableCount)
for ($i = 0; $i -lt $take; $i++) { $null = $hostValues.Add([uint32]($firstHost + $i)) }
}
else {
$take = [int][Math]::Min($usableCount, $MaxScanHosts)
for ($i = 0; $i -lt $take; $i++) { $null = $hostValues.Add([uint32]($firstHost + $i)) }
}
return @($hostValues)
}
11 changes: 11 additions & 0 deletions NetDiagram-PS/Private/New-EmptyTopology.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function New-EmptyTopology {
<#
.SYNOPSIS
Creates an empty topology object
#>
[pscustomobject]@{
Nodes = @()
Edges = @()
Subnets = @()
}
}
50 changes: 50 additions & 0 deletions NetDiagram-PS/Private/Resolve-SnmpCredentialConfig.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
function Resolve-SnmpCredentialConfig {
param(
[Parameter(Mandatory)][psobject]$SnmpMap,
[Parameter(Mandatory)][string]$IPAddress
)

$defaultConfig = $null
$cidrMatches = @()

foreach ($entry in $SnmpMap.PSObject.Properties) {
if ($entry.Name -eq 'Default') {
$defaultConfig = $entry.Value
continue
}

if ($entry.Name -notmatch '^(\d{1,3}(?:\.\d{1,3}){3})\/(\d{1,2})$') {
throw "Invalid SNMP credential-map CIDR '$($entry.Name)'. Expected an IPv4 CIDR or Default."
}

$prefixLength = [int]$matches[2]
$networkAddress = $null
if ($prefixLength -gt 32 -or
-not [System.Net.IPAddress]::TryParse($matches[1], [ref]$networkAddress) -or
$networkAddress.AddressFamily -ne [System.Net.Sockets.AddressFamily]::InterNetwork) {
throw "Invalid SNMP credential-map CIDR '$($entry.Name)'. Expected an IPv4 CIDR or Default."
}

if (Test-IPInSubnet -IP $IPAddress -CIDR $entry.Name) {
$cidrMatches += [pscustomobject]@{
CIDR = $entry.Name
PrefixLength = $prefixLength
Config = $entry.Value
}
}
}

$bestMatch = $cidrMatches | Sort-Object PrefixLength -Descending | Select-Object -First 1
if ($bestMatch) {
return $bestMatch
}
if ($null -ne $defaultConfig) {
return [pscustomobject]@{
CIDR = 'Default'
PrefixLength = -1
Config = $defaultConfig
}
}

return $null
}
58 changes: 58 additions & 0 deletions NetDiagram-PS/Private/Test-IPInSubnet.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
function Test-IPInSubnet {
<#
.SYNOPSIS
Simple CIDR matching helper
#>
param([string]$IP, [string]$CIDR)

if ([string]::IsNullOrWhiteSpace($IP) -or [string]::IsNullOrWhiteSpace($CIDR)) {
return $false
}

if ($CIDR -notmatch '^(\d{1,3}(?:\.\d{1,3}){3})\/(\d{1,2})$') {
return $false
}

$networkAddressString = $matches[1]
$prefixLength = [int]$matches[2]

if ($prefixLength -lt 0 -or $prefixLength -gt 32) {
return $false
}

try {
$ipAddress = [System.Net.IPAddress]::Parse($IP)
$networkAddress = [System.Net.IPAddress]::Parse($networkAddressString)
}
catch {
return $false
}

if ($ipAddress.AddressFamily -ne [System.Net.Sockets.AddressFamily]::InterNetwork -or
$networkAddress.AddressFamily -ne [System.Net.Sockets.AddressFamily]::InterNetwork) {
return $false
}

$ipBytes = $ipAddress.GetAddressBytes()
$networkBytes = $networkAddress.GetAddressBytes()

if ([System.BitConverter]::IsLittleEndian) {
[Array]::Reverse($ipBytes)
[Array]::Reverse($networkBytes)
}

$ipValue = [System.BitConverter]::ToUInt32($ipBytes, 0)
$networkValue = [System.BitConverter]::ToUInt32($networkBytes, 0)

# Build the prefix mask. Compute in UInt64 to avoid the int overflow that
# '[uint32]0xFFFFFFFF -shl n' triggers (0xFFFFFFFF parses as int -1), then
# truncate back to 32 bits.
$mask = if ($prefixLength -eq 0) {
[uint32]0
}
else {
[uint32]((([uint64]4294967295) -shl (32 - $prefixLength)) -band [uint64]4294967295)
}

return (($ipValue -band $mask) -eq ($networkValue -band $mask))
}
31 changes: 31 additions & 0 deletions NetDiagram-PS/Private/Wait-DnsLookupTask.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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
}
}
Loading
Loading