@@ -337,6 +337,11 @@ function Get-SnmpNeighbors {
337337 Uses credential map to resolve community strings from SecretManagement.
338338 Adds discovered edges to the topology with L2-SNMP confidence.
339339
340+ CREDENTIAL MATCHING: The most specific matching IPv4 CIDR wins regardless of
341+ JSON property order. Default is used only when no CIDR matches. If the selected
342+ entry has no available community secret, the node is skipped unless -TryPublic
343+ was explicitly supplied.
344+
340345 NODE ELIGIBILITY: By default every node is queried EXCEPT nodes that have been
341346 explicitly marked unreachable (Reachable -eq $false). Nodes with unknown
342347 reachability (Reachable -eq $null, e.g. straight after Import-Inventory) ARE
@@ -453,16 +458,14 @@ All SNMP attempts will be logged to verbose output.
453458 $source = ' CredentialMap'
454459
455460 if ($credMap.PSObject.Properties [' snmp' ] -and $credMap.snmp ) {
456- foreach ($cidrEntry in $credMap.snmp.PSObject.Properties ) {
457- $cidr = $cidrEntry.Name
458- $config = $cidrEntry.Value
459-
460- # Simple CIDR matching (for MVP, just match Default or exact match)
461- if ($cidr -eq ' Default' -or (Test-IPInSubnet - IP $node.IP - CIDR $cidr )) {
462- if ($config.PSObject.Properties [' communitySecret' ]) {
463- $communitySecret = $config.communitySecret
464- break
465- }
461+ $credentialMatch = Resolve-SnmpCredentialConfig - SnmpMap $credMap.snmp - IPAddress $node.IP
462+ if ($credentialMatch ) {
463+ $source = " CredentialMap:$ ( $credentialMatch.CIDR ) "
464+ if ($credentialMatch.Config.PSObject.Properties [' communitySecret' ]) {
465+ $communitySecret = $credentialMatch.Config.communitySecret
466+ }
467+ else {
468+ Write-Warning " SNMP credential entry '$ ( $credentialMatch.CIDR ) ' has no communitySecret; skipping configured credential for $ ( $node.IP ) "
466469 }
467470 }
468471 }
@@ -573,6 +576,57 @@ All SNMP attempts will be logged to verbose output.
573576 }
574577}
575578
579+ function Resolve-SnmpCredentialConfig {
580+ param (
581+ [Parameter (Mandatory )][psobject ]$SnmpMap ,
582+ [Parameter (Mandatory )][string ]$IPAddress
583+ )
584+
585+ $defaultConfig = $null
586+ $cidrMatches = @ ()
587+
588+ foreach ($entry in $SnmpMap.PSObject.Properties ) {
589+ if ($entry.Name -eq ' Default' ) {
590+ $defaultConfig = $entry.Value
591+ continue
592+ }
593+
594+ if ($entry.Name -notmatch ' ^(\d{1,3}(?:\.\d{1,3}){3})\/(\d{1,2})$' ) {
595+ throw " Invalid SNMP credential-map CIDR '$ ( $entry.Name ) '. Expected an IPv4 CIDR or Default."
596+ }
597+
598+ $prefixLength = [int ]$matches [2 ]
599+ $networkAddress = $null
600+ if ($prefixLength -gt 32 -or
601+ -not [System.Net.IPAddress ]::TryParse($matches [1 ], [ref ]$networkAddress ) -or
602+ $networkAddress.AddressFamily -ne [System.Net.Sockets.AddressFamily ]::InterNetwork) {
603+ throw " Invalid SNMP credential-map CIDR '$ ( $entry.Name ) '. Expected an IPv4 CIDR or Default."
604+ }
605+
606+ if (Test-IPInSubnet - IP $IPAddress - CIDR $entry.Name ) {
607+ $cidrMatches += [pscustomobject ]@ {
608+ CIDR = $entry.Name
609+ PrefixLength = $prefixLength
610+ Config = $entry.Value
611+ }
612+ }
613+ }
614+
615+ $bestMatch = $cidrMatches | Sort-Object PrefixLength - Descending | Select-Object - First 1
616+ if ($bestMatch ) {
617+ return $bestMatch
618+ }
619+ if ($null -ne $defaultConfig ) {
620+ return [pscustomobject ]@ {
621+ CIDR = ' Default'
622+ PrefixLength = -1
623+ Config = $defaultConfig
624+ }
625+ }
626+
627+ return $null
628+ }
629+
576630function Test-IPInSubnet {
577631 <#
578632 . SYNOPSIS
0 commit comments