-
-
Save taoski/f1cfeff5e98690147f943a62f9b5ef50 to your computer and use it in GitHub Desktop.
CIDR to IP range conversion using PowerShell and ping range of IP addresses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# calculate IP address range from CIDR notation | |
# https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing | |
function cidrToIpRange { | |
param ( | |
[string] $cidrNotation | |
) | |
$addr, $maskLength = $cidrNotation -split '/' | |
[int]$maskLen = 0 | |
if (-not [int32]::TryParse($maskLength, [ref] $maskLen)) { | |
throw "Cannot parse CIDR mask length string: '$maskLen'" | |
} | |
if (0 -gt $maskLen -or $maskLen -gt 32) { | |
throw "CIDR mask length must be between 0 and 32" | |
} | |
$ipAddr = [Net.IPAddress]::Parse($addr) | |
if ($ipAddr -eq $null) { | |
throw "Cannot parse IP address: $addr" | |
} | |
if ($ipAddr.AddressFamily -ne [Net.Sockets.AddressFamily]::InterNetwork) { | |
throw "Can only process CIDR for IPv4" | |
} | |
$shiftCnt = 32 - $maskLen | |
$mask = -bnot ((1 -shl $shiftCnt) - 1) | |
$ipNum = [Net.IPAddress]::NetworkToHostOrder([BitConverter]::ToInt32($ipAddr.GetAddressBytes(), 0)) | |
$ipStart = ($ipNum -band $mask) + 1 | |
$ipEnd = ($ipNum -bor (-bnot $mask)) - 1 | |
# return as tuple of strings: | |
([BitConverter]::GetBytes([Net.IPAddress]::HostToNetworkOrder($ipStart)) | ForEach-Object { $_ } ) -join '.' | |
([BitConverter]::GetBytes([Net.IPAddress]::HostToNetworkOrder($ipEnd)) | ForEach-Object { $_ } ) -join '.' | |
} | |
$ipToUse = Read-Host "Enter IP address and subnet mask (e.g., 192.168.1.0/24):" | |
$start, $end = cidrToIpRange $ipToUse | |
Write-Host "Start: $start, end: $end" | |
# Function to convert IP address to integer | |
function ConvertTo-Int32 { | |
param ( | |
[string]$IPAddress | |
) | |
$ipBytes = $IPAddress.Split('.') | |
$intIP = 0 | |
for ($i = 0; $i -lt 4; $i++) { | |
$intIP += [int]$ipBytes[$i] * [math]::Pow(256, 3 - $i) | |
} | |
return $intIP | |
} | |
# Function to convert integer to IP address | |
function ConvertTo-IPAddress { | |
param ( | |
[int]$IntIP | |
) | |
$ipBytes = @() | |
for ($i = 0; $i -lt 4; $i++) { | |
$ipBytes += [math]::Floor($IntIP / [math]::Pow(256, 3 - $i)) | |
$IntIP = $IntIP % [math]::Pow(256, 3 - $i) | |
} | |
return $ipBytes -join '.' | |
} | |
# Function to get IP range | |
function Get-IPRange { | |
param ( | |
[string]$StartIPAddress, | |
[string]$EndIPAddress | |
) | |
$startInt = ConvertTo-Int32 -IPAddress $StartIPAddress | |
$endInt = ConvertTo-Int32 -IPAddress $EndIPAddress | |
$IPRange = @() | |
for ($i = $startInt; $i -le $endInt; $i++) { | |
$IPRange += ConvertTo-IPAddress -IntIP $i | |
} | |
return $IPRange | |
} | |
# Prompt for IP address range | |
$startIP = $start | |
$endIP = $end | |
# Get IP address range | |
$ipRange = Get-IPRange -StartIPAddress $startIP -EndIPAddress $endIP | |
# Ping each IP address in the range | |
foreach ($ip in $ipRange) { | |
$pingResult = Test-Connection -ComputerName $ip -Count 1 -Quiet | |
if ($pingResult) { | |
Write-Host "Ping successful: $ip" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to prompt for IP range and then ping IP addresses in that range, only reporting positive responses.