'Powershell IP address range
I need to help with my code which is write in Powershell. Program should generate IP addresses in range. For example from 10.4.254.250 to 10.4.255.255.
When I have the same subnet (from 10.4.255.x to 10.4.255.x), all is correct. Problem starts when I have different subnet (from 10.4.254.250 to 10.4.255.255).
Output is invalid. Try it please. Thank you, for your help.
Correct output should be, that ip address which is 10.4.255.X starts from 1. Now starts from 250 to 255.
I need to get all ip addresses from variable $from to variable $to. When IP address in the same subnet $from = "10.4.255.1" $to = "10.4.255.1" all is correct. Problem starts, when different subnet $from = "10.4.254.250" $to = "10.4.255.255"
Look at my code bellow:
$from = "10.4.254.250"
$to = "10.4.255.255"
$Ip_Adresa_Od = $from -split "\."
$Ip_Adresa_Do = $to -split "\."
foreach ($Ip_Adresa_A in $Ip_Adresa_Od[0]..$Ip_Adresa_Do[0])
{
foreach ($Ip_Adresa_B in $Ip_Adresa_Od[1]..$Ip_Adresa_Do[1])
{
foreach ($Ip_Adresa_C in $Ip_Adresa_Od[2]..$Ip_Adresa_Do[2])
{
foreach ($Ip_Adresa_D in $Ip_Adresa_Od[3]..$Ip_Adresa_Do[3])
{
$Ip_Adresa_Pocitace = "$Ip_Adresa_A.$Ip_Adresa_B.$Ip_Adresa_C.$Ip_Adresa_D"
$Ip_Adresa_Pocitace
}
}
}
}
Wrong output is:
10.4.254.250
10.4.254.251
10.4.254.252
10.4.254.253
10.4.254.254
10.4.254.255
10.4.255.250
10.4.255.251
10.4.255.252
10.4.255.253
10.4.255.254
10.4.255.255
Solution 1:[1]
You have to convert your IP address to an integer and then in each iteration of a for loop convert the integer to a byte array:
$from = "10.4.254.250"
$to = "10.4.255.255"
$Ip_Adresa_Od = $from -split "\."
$Ip_Adresa_Do = $to -split "\."
#change endianness
[array]::Reverse($Ip_Adresa_Od)
[array]::Reverse($Ip_Adresa_Do)
#convert octets to integer
$start=[bitconverter]::ToUInt32([byte[]]$Ip_Adresa_Od,0)
$end=[bitconverter]::ToUInt32([byte[]]$Ip_Adresa_Do,0)
for ($ip=$start; $ip -lt $end; $ip++)
{
#convert integer back to byte array
$get_ip=[bitconverter]::getbytes($ip)
#change endianness
[array]::Reverse($get_ip)
$new_ip=$get_ip -join "."
$new_ip
}
Solution 2:[2]
Working with IP addresses and ranges is complicated, and something I try to avoid if a program/software I am using does it already. Here are some functions that I wrote a while back that convert the addresses to decimal values, that are easier to manipulate. There are probably better, more precise solutions than this, but it will also return a range based off an address with a Subnet address or CIDR mask too. It should also cover the case @vonPryz mentioned where the addresses are across .24
CIDR ranges.
function Find-IPRange {
<#
.SYNOPSIS
Determines all the IP address in a given range or subnet.
.DESCRIPTION
This function can evaluate a set of addresses based of the following three options:
Range - What IP addresses are between this and that address
Mask - What are the IP addresses given a particular IP address and mask, i.e. 24, 25.
Subnet - What are the IP addresses given a particular IP address and subnet address, i.e 255.255.0.0, 255.255.255.192
You have to specify an IP address to use the subnet and mask options. For the range you have to specify two addresses.
.PARAMETER Start
Start address of an IP range
.PARAMETER End
End address of an IP range
.PARAMETER IP
Any valid ip address
.PARAMETER Subnet
A valid Subnet IP address i.e. 255.255.255.0, 255.255.0.0
.PARAMETER Mask
A valid net mask from 0 to 32
.EXAMPLE
Find-IPRange -IP 192.168.0.4 -mask 30
.EXAMPLE
Find-IPRange -Start 192.168.1.250 -End 192.168.2.5
.EXAMPLE
Find-IPRange -IP 10.100.100.10 -Subnet 255.255.255.240
#>
[CmdletBinding(DefaultParameterSetName = "Range")]
Param (
[Parameter(Mandatory = $true, ParameterSetName = "Range")]
[System.Net.IPAddress]
$Start,
[Parameter(Mandatory = $true, ParameterSetName = "Range")]
[System.Net.IPAddress]
$End,
[Parameter(Mandatory = $true, ParameterSetName = "Mask")]
[Parameter(Mandatory = $true, ParameterSetName = "Subnet")]
[System.Net.IPAddress]
$IP,
[Parameter(Mandatory = $true, ParameterSetName = "Subnet")]
[System.Net.IPAddress]
$Subnet,
[Parameter(Mandatory = $true, ParameterSetName = "Mask")]
[ValidateRange(0, 32)]
[System.Int32]
$Mask,
[Parameter(ParameterSetName = "Mask")]
[Parameter(ParameterSetName = "Subnet")]
[System.Management.Automation.SwitchParameter]
$ReturnRange
)
Begin {
# If the user specifies a mask, then convert it to a subnet ip address
if ($Mask) {
$Binary = ("1" * $Mask) + ("0" * (32 - $Mask))
$Decimal = [System.Convert]::ToInt64($Binary, 2)
[System.Net.IPAddress]$Subnet = ConvertFrom-IntToIP -Decimal $Decimal
}
}
Process {
# If we're looking at a subnet, we need to establish the start address and the broadcast address for it. We're using bitwise operators to do this.
if ($PSCmdlet.ParameterSetName -ne "Range") {
# Compare bits where both are a match using the bitwise AND operator
[System.Net.IPAddress]$SubnetAddr = $Subnet.Address -band $IP.Address
# Flip the subnet mask i.e. 0.0.0.255 for 255.255.255.0 by using the bitwise XOR operator and then compare against a bitwise OR operator
[System.Net.IPAddress]$Broadcast = ([System.Net.IPAddress]'255.255.255.255').Address -bxor $Subnet.Address -bor $SubnetAddr.Address
# Return the start and end of a subnet only if requested
if ($ReturnRange) { return $SubnetAddr, $Broadcast }
# Convert the start and end of the ranges to integers
$RangeStart = ConvertFrom-IPToInt -ip $SubnetAddr.IPAddressToString
$RangeEnd = ConvertFrom-IPToInt -ip $Broadcast.IPAddressToString
}
else {
$RangeStart = ConvertFrom-IPToInt -ip $Start.IPAddressToString
$RangeEnd = ConvertFrom-IPToInt -ip $End.IPAddressToString
}
# Loop through the points between the start and end of the ranges and convert them back to IP addresses
for ($Addr = $RangeStart; $Addr -le $RangeEnd; $Addr ++) { ConvertFrom-IntToIP -Decimal $Addr }
}
End {
}
}
function ConvertFrom-IPToInt {
<#
.SYNOPSIS
Converts an IP address to an Int64 value.
.DESCRIPTION
Converts an IP address to an Int64 value.
.PARAMETER IP
A valid IP address to be converted to an integer
.EXAMPLE
ConvertFrom-IPToInt -IP 192.168.0.1
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]
[System.Net.IPAddress]
$IP
)
Begin {
}
Process {
# Split the IP address in to octets
$Octets = $IP -split "\."
# Multiply the octets based on the maximum number of addresses each octet provides.
[System.Int64]$Decimal = ([System.Int32]$Octets[0] * [System.Math]::Pow(256, 3)) +
([System.Int32]$Octets[1] * [System.Math]::Pow(256, 2)) +
([System.Int32]$Octets[2] * 256) +
([System.Int32]$Octets[3])
}
End {
# Return the int64 value
$Decimal
}
}
function ConvertFrom-IntToIP {
<#
.SYNOPSIS
Converts an Int64 value to an IP address.
.DESCRIPTION
Converts an Int64 value to an IP address.
.PARAMETER Decimal
A decimal value for the IP Address to be converted
.EXAMPLE
ConvertFrom-IntToIP -Decimal 3232235521
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]
[System.Int64]
$Decimal
)
Begin {
# Initialise an array for the octets
$Octets = @()
}
Process {
# Work out first octet by dividing by the total number of addresses.
$Octets += [System.String]([System.Math]::Truncate($Decimal / [System.Math]::Pow(256, 3)))
# Work out second octet by the modulus of the first octets total number of addresses divided by the total number of address available for a class B subnet.
$Octets += [System.String]([System.Math]::Truncate(($Decimal % [System.Math]::Pow(256, 3)) / [System.Math]::Pow(256, 2)))
# Work out third octet by the modulus of the second octets total number of addresses divided by the total number of address available for a class C subnet.
$Octets += [System.String]([System.Math]::Truncate(($Decimal % [System.Math]::Pow(256, 2)) / 256))
# Work out fourth octet by the modulus of the third octets total number of addresses.
$Octets += [System.String]([System.Math]::Truncate($Decimal % 256))
# Join the strings to form the IP address
[System.Net.IPAddress]$IP = $Octets -join "."
}
End {
# Return the ip address object
$IP.IPAddressToString
}
}
DISCLAIMER: I am not a network engineer so please feel free to suggest any changes to how the addresses are converted to ints and back. This function also hasn't been through any unit testing, so there may be cases that exist where it does not work.
Example Output:
Find-IPRange -Start 10.4.254.250 -End 10.4.255.255
10.4.254.250
10.4.254.251
10.4.254.252
10.4.254.253
10.4.254.254
10.4.254.255
10.4.255.0
10.4.255.1
10.4.255.2
...truncated
10.4.255.249
10.4.255.250
10.4.255.251
10.4.255.252
10.4.255.253
10.4.255.254
10.4.255.255
Other uses:
Find-IPRange -IP 192.168.0.4 -Mask 28
192.168.0.0
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
192.168.0.6
192.168.0.7
192.168.0.8
192.168.0.9
192.168.0.10
192.168.0.11
192.168.0.12
192.168.0.13
192.168.0.14
192.168.0.15
Find-IPRange -IP 192.168.0.4 -Subnet 255.255.255.252
192.168.0.4
192.168.0.5
192.168.0.6
192.168.0.7
Solution 3:[3]
I hope I understand your question. I believe you would like to restart the counter on the 4th octet back to 1 once the 3rd octet iterates from 254 to 255? There's probably a better way to do this but for now hopefully this works. I've added an if statement that resets the range once the final 10.4.254.255 ip is reached. This will allow your loop to include the 10.4.255.x range starting from 1 in the 4th octet until 255 is reached. The while loop condition will be set to false once the final 10.4.255.255 IP is reached and exit. I hope this helps and provides the desired result.
$from = "10.4.254.250"
$to = "10.4.254.255"
$Ip_Adresa_Od = $from -split "\."
$Ip_Adresa_Do = $to -split "\."
$run = "true";
while($run -eq "true")
{
if($Ip_Adresa_Pocitace -eq "10.4.254.255")
{
$from = "10.4.255.1"
$to = "10.4.255.255"
$Ip_Adresa_Od = $from -split "\."
$Ip_Adresa_Do = $to -split "\."
}
foreach ($Ip_Adresa_C in $Ip_Adresa_Od[2]..$Ip_Adresa_Do[2])
{
foreach ($Ip_Adresa_D in $Ip_Adresa_Od[3]..$Ip_Adresa_Do[3])
{
$Ip_Adresa_Pocitace = "10.4.$Ip_Adresa_C.$Ip_Adresa_D"
$Ip_Adresa_Pocitace
if($Ip_Adresa_Pocitace -eq "10.4.255.255")
{
$run = "false";
}
}
}
}
Results:
10.4.254.250
10.4.254.251
10.4.254.252
10.4.254.253
10.4.254.254
10.4.254.255
10.4.255.1
10.4.255.2
10.4.255.3
...
10.4.255.249
10.4.255.250
10.4.255.251
10.4.255.252
10.4.255.253
10.4.255.254
10.4.255.255Solution w/ new parameters.
# Orininal Parameters
# $from = "10.4.254.250"
# $to = "10.4.254.255"
$from = "10.4.253.250"
$to = "10.4.253.255"
$Ip_Adresa_Od = $from -split "\."
$Ip_Adresa_Do = $to -split "\."
$run = "true";
while($run -eq "true")
{
if($Ip_Adresa_Pocitace -eq "10.4.253.255")
{
# Orininal Parameters
# $from = "10.4.255.1"
# $to = "10.4.255.255"
$from = "10.4.254.1"
$to = "10.4.254.255"
$end = $to
$Ip_Adresa_Od = $from -split "\."
$Ip_Adresa_Do = $to -split "\."
}
foreach ($Ip_Adresa_C in $Ip_Adresa_Od[2]..$Ip_Adresa_Do[2])
{
foreach ($Ip_Adresa_D in $Ip_Adresa_Od[3]..$Ip_Adresa_Do[3])
{
$Ip_Adresa_Pocitace = "10.4.$Ip_Adresa_C.$Ip_Adresa_D"
$Ip_Adresa_Pocitace
if($Ip_Adresa_Pocitace -eq $end)
{
$run = "false";
}
}
}
}
Results:
10.4.253.250
10.4.253.251
10.4.253.252
10.4.253.253
10.4.253.254
10.4.253.255
10.4.254.1
10.4.254.2
10.4.254.3
...
10.4.254.253
10.4.254.254
10.4.254.255
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Antidisestablishmentarianism |
Solution 2 | |
Solution 3 |