Skip to content

Commit 7c07f81

Browse files
committed
Quick subnet check fix
1 parent 324aafa commit 7c07f81

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

Network_Configuration.ps1

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -730,20 +730,29 @@ function Test-ValidSubnetMask {
730730

731731
if ([string]::IsNullOrWhiteSpace($SubnetInput)) { return $false }
732732

733-
# Check if it's a prefix length (8-30 or /8-/30)
734-
if ($SubnetInput -match "^/?([8-9]|[12][0-9]|30)$") {
733+
# Check if it's a prefix length (8-32 or /8-/32)
734+
if ($SubnetInput -match "^/?([8-9]|[12][0-9]|3[0-2])$") {
735735
return $true
736736
}
737737

738738
# Check if it's a valid subnet mask notation
739739
if ($SubnetInput -match "^\d+(\.\d+){3}$") {
740740
try {
741-
$mask = [System.Net.IPAddress]::Parse($SubnetInput)
742-
# Get mask bytes for validation
743-
$null = $mask.GetAddressBytes()
741+
$octets = $SubnetInput -split '\.'
742+
743+
# Validate each octet is 0-255
744+
foreach ($octet in $octets) {
745+
$num = [int]$octet
746+
if ($num -lt 0 -or $num -gt 255) {
747+
return $false
748+
}
749+
}
744750

745751
# Convert to binary and check if it's a valid subnet mask
746-
$binaryMask = [Convert]::ToString($mask.Address, 2).PadLeft(32, '0')
752+
$binaryMask = ""
753+
foreach ($octet in $octets) {
754+
$binaryMask += [Convert]::ToString([int]$octet, 2).PadLeft(8, '0')
755+
}
747756

748757
# Valid subnet mask should have consecutive 1s followed by consecutive 0s
749758
if ($binaryMask -match "^1*0*$" -and $binaryMask -ne "00000000000000000000000000000000") {

0 commit comments

Comments
 (0)