-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathCIDRtoIpRange.ps1
More file actions
312 lines (254 loc) · 9.37 KB
/
Copy pathCIDRtoIpRange.ps1
File metadata and controls
312 lines (254 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
param(
[Parameter(Mandatory=$True, Position=0, ValueFromPipeline=$false)]
[System.String]
$IPAddressJsonFilePath,
[Parameter(Mandatory=$True, Position=1, ValueFromPipeline=$false)]
[System.String]
$ServiceTagName
)
# https://blog.tyang.org/2011/05/01/powershell-functions-get-ipv4-network-start-and-end-address/
Function Get-IPV4NetworkStartIP ($strNetwork)
{
$StrNetworkAddress = ($strNetwork.split("/"))[0]
[int]$NetworkLength = ($strNetwork.split("/"))[1]
$NetworkIP = ([System.Net.IPAddress]$StrNetworkAddress).GetAddressBytes()
[Array]::Reverse($NetworkIP)
$NetworkIP = ([System.Net.IPAddress]($NetworkIP -join ".")).Address
if ($NetworkLength -lt 32)
{
$StartIP = $NetworkIP +1
}
else
{
$StartIP = $NetworkIP
}
#Convert To Double
If (($StartIP.Gettype()).Name -ine "double")
{
$StartIP = [Convert]::ToDouble($StartIP)
}
$StartIP = [System.Net.IPAddress]$StartIP
Return $StartIP
}
# https://blog.tyang.org/2011/05/01/powershell-functions-get-ipv4-network-start-and-end-address/
Function Get-IPV4NetworkEndIP ($strNetwork)
{
$StrNetworkAddress = ($strNetwork.split("/"))[0]
[int]$NetworkLength = ($strNetwork.split("/"))[1]
$IPLength = 32-$NetworkLength
$NumberOfIPs = ([System.Math]::Pow(2, $IPLength)) -1
$NetworkIP = ([System.Net.IPAddress]$StrNetworkAddress).GetAddressBytes()
[Array]::Reverse($NetworkIP)
$NetworkIP = ([System.Net.IPAddress]($NetworkIP -join ".")).Address
$EndIP = $NetworkIP + $NumberOfIPs
If (($EndIP.Gettype()).Name -ine "double")
{
$EndIP = [Convert]::ToDouble($EndIP)
}
$EndIP = [System.Net.IPAddress]$EndIP
Return $EndIP
}
# Determine if an IP Address is IPv4
Function IsIPv4 ($strIpAddress)
{
$ip = [IpAddress]$strIpAddress
Return $ip.AddressFamily -eq "InterNetwork"
}
# Determine if an IP Address is IPv4
Function IsIPv6 ($strIpAddress)
{
$ip = [IpAddress]$strIpAddress
Return $ip.AddressFamily -eq "InterNetworkV6"
}
#https://www.powershellgallery.com/packages/PoshFunctions/2.2.3/Content/Functions%5CExpand-IPv6.ps1
function Expand-IPV6 {
<#
.SYNOPSIS
Takes an abbreviated IPv6 string and expands it fully
.DESCRIPTION
Takes an abbreviated IPv6 string and expands it fully
.PARAMETER IPv6
A string parameter that represents an IPv6 address. Aliased to 'Address'
.PARAMETER IncludeInput
Switch that will display the input parameter along with the result
.EXAMPLE
Expand-IPV6 'fe98::726d:daad:2afc:5393%18'
Would return:
FE98:0000:0000:0000:726D:DAAD:2AFC:0000
.EXAMPLE
Expand-IPV6 'fe98::726d:daad:2afc:5393'
Would return:
FE98:0000:0000:0000:726D:DAAD:2AFC:5393
.EXAMPLE
Expand-IPV6 -IPv6 '::1'
Would return:
0000:0000:0000:0000:0000:0000:0000:0001
.EXAMPLE
'::1', 'fe98::726d:daad:2afc:5393' | Expand-IPV6 -IncludeInput
OriginalIPv6 ExpandedIPv6
------------ ------------
::1 0000:0000:0000:0000:0000:0000:0000:0001
fe98::726d:daad:2afc:5393 FE98:0000:0000:0000:726D:DAAD:2AFC:5393
.NOTES
Source: https://badflyer.com/powershell-ipv4-to-ipv6/
Changes:
- added comment help
- minor formatting changes
- change IPv6 to string array
- added IncludeInput parameter
#>
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments','')]
param
(
[Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[Alias('Address')]
[string[]] $IPv6,
[switch] $IncludeInput
)
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
}
process {
foreach ($curIPv6 in $IPv6) {
$count = 0
$loc = -1
# Count the number of colons, and keep track of the double colon
for ($i = 0; $i -lt $curIPv6.Length; $i++) {
if ($curIPv6[$i] -eq ':') {
$count++
if (($i - 1) -ge 0 -and $curIPv6[$i - 1] -eq ':') {
$loc = $i
}
}
}
# If we didnt find a double colon and the count isn't 7, then throw an exception
if ($loc -lt 0 -and $count -ne 7) {
throw 'Invalid IPv6 Address'
}
# Add in any missing colons if we had a double
$cleaned = $curIPv6
if ($count -lt 7) {
$cleaned = $curIPv6.Substring(0, $loc) + (':' * (7 - $count)) + $curIPv6.Substring($loc)
}
# Parse current values in fill in new IP with hex numbers padded to 4 digits
$result = @()
foreach ($splt in $cleaned -split ':') {
$val = 0
$r = [int]::TryParse($splt, [System.Globalization.NumberStyles]::HexNumber, [System.Globalization.CultureInfo]::InvariantCulture, [ref]$val)
$result += ('{0:X4}' -f $val)
}
$result = $result -join ':'
if ($IncludeInput) {
New-Object -TypeName psobject -Property ([ordered] @{
OriginalIPv6 = $curIPv6
ExpandedIPv6 = $result
})
} else {
Write-Output -InputObject $result
}
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}
# https://stackoverflow.com/questions/75533520/how-to-convert-ip-number-to-ipv6-using-powershell/75535232#75535232
function Convert-NumberToIPv6
{
param(
[Parameter(Mandatory=$true)][bigInt]$ipv6Decimal
)
$ipv6Bytes = $ipv6Decimal.ToByteArray()
# pad to 16 bytes
[Array]::Resize([ref]$ipv6Bytes, 16)
# reverse the bytes
[Array]::Reverse($ipv6Bytes)
# provide a scope identifier to prevent "cannot find overload error"
$ipAddress = New-Object Net.IPAddress($ipv6Bytes, 0)
$ipAddress
}
Function GetNetworkAddress($strIpAddressRange)
{
Return ($strIpAddressRange.split("/"))[0]
}
$myJson = Get-Content $IPAddressJsonFilePath -Raw | ConvertFrom-Json
$ranges = $myJson.values | where {$_.id -eq $ServiceTagName}
write-Output "IP Ranges for $ServiceTagName below =>"
write-Output ""
$count = 1
foreach($range in $ranges.properties.addressPrefixes)
{
$networkAddress = GetNetworkAddress($range)
If (IsIPv4($networkAddress) -eq $True)
{
$start = Get-IPV4NetworkStartIP($range)
$end = Get-IPV4NetworkEndIP($range)
Write-Output ($start.IPAddressToString + " : " + $end.IPAddressToString)
}
elseif (IsIPv6($networkAddress) -eq $True)
{
$startIp = ($range -split '/')[0]
$startIpAddress = [System.Net.IPAddress]::Parse($startIP)
$startIpBytes = [System.Net.IPAddress]::Parse($startIP).GetAddressBytes()
[System.Array]::Reverse($startIpBytes)
$startIpAsInt = [bigint]$startIpBytes
# CIDR Range
$cidrRange = [bigint]([math]::pow(2, (128 - ($range -split '/')[1])))
[bigint]$endIpAsInt = $startIpAsInt + $cidrRange - 1
$endIpAddress = Convert-NumberToIPv6 $endIpAsInt
$expandedStartIpAddress = (Expand-IPV6 $startIpAddress.IPAddressToString).ToLower()
$expandedEndIpAddress = (Expand-IPV6 $endIpAddress.IPAddressToString).ToLower()
Write-Output ($expandedStartIpAddress + " : " + $expandedEndIpAddress)
}
$count = $count + 1
}
write-Output ""
write-Output "Sample Storage Account PowerShell Script =>"
write-Output ""
write-Output "`$storageAccountName = `"INSET_NAME_HERE`""
write-Output "`$resourceGroupName = `"INSET_NAME_HERE`""
foreach($range in $ranges.properties.addressPrefixes)
{
$networkAddress = GetNetworkAddress($range)
If (IsIPv4($networkAddress) -eq $True)
{
$newRange = $range -replace "/32"
}
elseif (IsIPv6($networkAddress) -eq $True)
{
$newRange = $range -replace "/128"
}
Write-Output ("Add-AzStorageAccountNetworkRule -ResourceGroupName `$resourceGroupName -Name `$storageAccountName -IPAddressOrRange " + $newRange)
}
write-Output ""
write-Output "Sample Azure Synapse PowerShell Script =>"
write-Output ""
write-Output "`$synapseWorkspaceName = `"INSET_NAME_HERE`""
$count = 1
foreach($range in $ranges.properties.addressPrefixes)
{
$networkAddress = GetNetworkAddress($range)
If (IsIPv4($networkAddress) -eq $True)
{
$start = Get-IPV4NetworkStartIP($range)
$end = Get-IPV4NetworkEndIP($range)
}
elseif (IsIPv6($networkAddress) -eq $True)
{
$startIp = ($range -split '/')[0]
$startIpAddress = [System.Net.IPAddress]::Parse($startIP)
$startIpBytes = [System.Net.IPAddress]::Parse($startIP).GetAddressBytes()
[System.Array]::Reverse($startIpBytes)
$startIpAsInt = [bigint]$startIpBytes
# CIDR Range
$cidrRange = [bigint]([math]::pow(2, (128 - ($range -split '/')[1])))
[bigint]$endIpAsInt = $startIpAsInt + $cidrRange - 1
$endIpAddress = Convert-NumberToIPv6 $endIpAsInt
$start = (Expand-IPV6 $startIpAddress.IPAddressToString).ToLower()
$end = (Expand-IPV6 $endIpAddress.IPAddressToString).ToLower()
}
$name = ($ServiceTagName + "-" + $count)
Write-Output ("New-AzSynapseFirewallRule -WorkspaceName `$synapseWorkspaceName -Name $name -StartIpAddress `"$start`" -EndIpAddress `"$end`"")
$count = $count + 1
}