-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVNet.psm1
More file actions
238 lines (184 loc) · 8.84 KB
/
VNet.psm1
File metadata and controls
238 lines (184 loc) · 8.84 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
class Subnet {
[string] $CIDR
[NET.IPAddress] $Start
[NET.IPAddress] $End
[string] ToString() {
return $this.CIDR
}
}
class VNetRange {
[NET.IPAddress] $Start
[NET.IPAddress] $End
[string] ToString() {
return $this.Start + "-" + $this.End
}
}
class Free {
[NET.IPAddress] $Start
[NET.IPAddress] $End
[int] $Size
[string[]] $CIDRAvailable
[string] ToString() {
return $this.Size
}
}
class VNetSummary {
[string] $VNetStart
[string] $VNetEnd
[VNetRange[]] $VNetRanges
[free[]] $Available
[subnet[]] $Subnets
}
<#
.SYNOPSIS
List subnets for a VNet and show any unallocated gaps
.DESCRIPTION
Returns a list of subnets in use for a particular VNET and also any gaps available
.PARAMETER ResourceGroup
The name of the resource group that contains a virtual network
.PARAMETER VNetName
The name of the virtual network to query for subnets
.NOTES
This function uses Azure PowerShell cmdlets, so assumes that you've already run Connect-AzAccount to sign in to Azure.
.EXAMPLE
$result = Find-FreeSubnets -ResourceGroup rg-freesubnet-australiaeast -VNetName vnet-freesubnet-australiaeast
Displaying $result:
VNet Start VNet End Available Subnets
---------- -------- --------- -------
10.0.0.0 10.0.255.255 {48, 8} {10.0.0.0/24, 10.0.1.0/28, 10.0.1.64/28, 10.0.1.88/29}
If the virtual network has multiple address ranges, VNetStart and VNetEnd are those of the last range.
In that case, 'Available' and 'Subnets' contain all available ranges and all subnets of all address ranges.
'VNetRanges' contains the start and end of each address range in the virtual network.
And the 'Available' property contains:
Start End Size Available ranges
----- --- ---- ----------------
10.0.1.16 10.0.1.63 48 {10.0.1.16/28, 10.0.1.32/27, 10.0.1.32/28, 10.0.1.48/28}
10.0.1.80 10.0.1.87 8
The 'Available ranges' array is a list of one or more CIDR ranges that could utilise the available IP addresses.
#>
function Find-FreeSubnets {
[CmdletBinding()]
[OutputType([VNetSummary])]
param (
[string]
$ResourceGroup,
[string]
$VNetName
)
begin {
# https://gist.github.com/davidjenni/7eb707e60316cdd97549b37ca95fbe93
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)
$ipEnd = ($ipNum -bor (-bnot $mask))
# return as tuple of strings:
$bytes = [BitConverter]::GetBytes([Net.IPAddress]::HostToNetworkOrder($ipStart))
New-Object -TypeName "Net.IPAddress" -argumentList (, $bytes)
$bytes = [BitConverter]::GetBytes([Net.IPAddress]::HostToNetworkOrder($ipEnd))
New-Object Net.IPAddress -ArgumentList (, $bytes)
}
$vnet = Get-AzVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroup
$result = [VNetSummary]::new()
$vnetAddressPrefixes = $vnet.AddressSpace.AddressPrefixes
foreach($addressPrefix in $vnetAddressPrefixes)
{
[Net.IPAddress] $vnetStart, [Net.IPAddress] $vnetEnd = cidrToIpRange $addressPrefix
$result.VNetStart = $vnetStart
$result.VNetEnd = $vnetEnd
$vnetRange = [VNetRange]::new()
$vnetRange.Start = $vnetStart
$vnetRange.End = $vnetEnd
$result.VNetRanges += $vnetRange
# Create fake subnet immediately following the end of the VNet
$ipNum = [Net.IPAddress]::NetworkToHostOrder([BitConverter]::ToInt32($vnetEnd.GetAddressBytes(), 0)) + 1
$bytes = [BitConverter]::GetBytes([Net.IPAddress]::HostToNetworkOrder($ipNum))
[Net.IPAddress] $afterLastAvailable = New-Object Net.IPAddress -ArgumentList (, $bytes)
$afterLastAvailableCidr = "$afterLastAvailable/31"
# create fake subnet immediately before the start of the VNet
$ipNum = [Net.IPAddress]::NetworkToHostOrder([BitConverter]::ToInt32($vnetStart.GetAddressBytes(), 0)) - 1
$bytes = [BitConverter]::GetBytes([Net.IPAddress]::HostToNetworkOrder($ipNum))
[Net.IPAddress] $beforeFirstAvailable = New-Object Net.IPAddress -ArgumentList (, $bytes)
$beforeFirstAvailableCidr = "$beforeFirstAvailable/31"
$sorted = @($beforeFirstAvailableCidr) + @($vnet.Subnets.AddressPrefix) + @($afterLastAvailableCidr) | Sort-Object -Property {
$addr, $maskLength = $_ -split '/'
$ip = ([Net.IPAddress] $addr)
$ipNum = [Net.IPAddress]::NetworkToHostOrder([BitConverter]::ToInt32($ip.GetAddressBytes(), 0))
$ipNum
}
$maskToAddresses = @{ 28 = 16; 27 = 32; 26 = 64; 25 = 128; 24 = 256 }
$addressToStarts = @{
}
$maskToAddresses.Values | ForEach-Object {
$addressToStarts.Add($_, $(for ($i = 0; $i -lt 255; $i += $_) { $i }))
}
$nextAvailableNum = 0
$notFirst = $false
foreach ($cidr in $sorted) {
$start, $end = cidrToIpRange $cidr
$startNum = [Net.IPAddress]::NetworkToHostOrder([BitConverter]::ToInt32($start.GetAddressBytes(), 0))
if ($notFirst -and $nextAvailableNum -ne $startNum ) {
$bytes = [BitConverter]::GetBytes([Net.IPAddress]::HostToNetworkOrder($nextAvailableNum))
$nextAvailable = New-Object Net.IPAddress -ArgumentList (, $bytes)
$bytes = [BitConverter]::GetBytes([Net.IPAddress]::HostToNetworkOrder($startNum - 1))
$lastAvailable = New-Object Net.IPAddress -ArgumentList (, $bytes)
$free = [Free]::new()
$free.Start = $nextAvailable
$free.End = $lastAvailable
$free.Size = $startNum - $nextAvailableNum
$result.Available += $free
for ($i = $nextAvailableNum; $i -lt $startNum; $i += 8) {
$bytes = [BitConverter]::GetBytes([Net.IPAddress]::HostToNetworkOrder($i))
$freeIp = New-Object Net.IPAddress -ArgumentList (, $bytes)
foreach ($mask in ($maskToAddresses.Keys | Sort-Object)) {
$address = $maskToAddresses[$mask]
if ($addressToStarts[$address] -contains $bytes[3] ) {
$freeCidr = $freeIp.IPAddressToString + "/$mask"
# Check this doesn't overlap next
$possibleFreeStart, $possibleFreeEnd = cidrToIpRange $freeCidr
$possibleFreeEndNum = [Net.IPAddress]::NetworkToHostOrder([BitConverter]::ToInt32($possibleFreeEnd.GetAddressBytes(), 0))
if ($possibleFreeEndNum -lt $startNum) {
$free.CIDRAvailable += $freeCidr
}
}
}
}
}
$notFirst = $true
if ($cidr -ne $afterLastAvailableCidr -and $cidr -ne $beforeFirstAvailableCidr) {
$subnet = [Subnet]::new()
$subnet.CIDR = $cidr
$subnet.Start = $start
$subnet.End = $end
$result.Subnets += $subnet
}
$nextAvailableNum = [Net.IPAddress]::NetworkToHostOrder([BitConverter]::ToInt32($end.GetAddressBytes(), 0)) + 1
$bytes = [BitConverter]::GetBytes([Net.IPAddress]::HostToNetworkOrder($ipEnd))
}
}
if ($end -ne $vnetEnd) {
$bytes = [BitConverter]::GetBytes([Net.IPAddress]::HostToNetworkOrder($nextAvailableNum))
$nextAvailable = New-Object Net.IPAddress -ArgumentList (, $bytes)
}
$result
}
}