-
Notifications
You must be signed in to change notification settings - Fork 0
/
IP_Subnet_Calculate.ps1
100 lines (81 loc) · 3.4 KB
/
IP_Subnet_Calculate.ps1
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
<#
# Calculate the subnet and get details of ip range with class.
# https://github.com/VSKUMBHANI/PowerShell
Example
Get-Subnet -IP 192.168.1.10/24
#>
Function Get-Subnet {
param (
[parameter(ValueFromPipeline)]
[String]
$IP,
[ValidateRange(0, 32)]
[int]
$MaskBits,
[switch]
$SkipHosts
)
Begin {
Function Convert-IPtoINT64 ($ip) {
$octets = $ip.split(".")
[int64]([int64]$octets[0] * 16777216 + [int64]$octets[1] * 65536 + [int64]$octets[2] * 256 + [int64]$octets[3])
}
Function Convert-INT64toIP ([int64]$int) {
(([math]::truncate($int / 16777216)).tostring() + "." + ([math]::truncate(($int % 16777216) / 65536)).tostring() + "." + ([math]::truncate(($int % 65536) / 256)).tostring() + "." + ([math]::truncate($int % 256)).tostring() )
}
If (-not $IP -and -not $MaskBits) {
$LocalIP = (Get-NetIPAddress | Where-Object {$_.AddressFamily -eq 'IPv4' -and $_.PrefixOrigin -ne 'WellKnown'})
$IP = $LocalIP.IPAddress
$MaskBits = $LocalIP.PrefixLength
}
}
Process {
If ($IP -match '/\d') {
$IPandMask = $IP -Split '/'
$IP = $IPandMask[0]
$MaskBits = $IPandMask[1]
}
$IPAddr = [Net.IPAddress]::Parse($IP)
$Class = Switch ($IP.Split('.')[0]) {
{$_ -in 0..127} { 'A' }
{$_ -in 128..191} { 'B' }
{$_ -in 192..223} { 'C' }
{$_ -in 224..239} { 'D' }
{$_ -in 240..255} { 'E' }
}
If (-not $MaskBits) {
$MaskBits = Switch ($Class) {
'A' { 8 }
'B' { 16 }
'C' { 24 }
default { Throw 'Subnet mask size was not specified and could not be inferred.' }
}
Write-Warning "Subnet mask size was not specified. Using default subnet size for a Class $Class network of /$MaskBits."
}
If ($MaskBits -lt 16 -and -not $SkipHosts) {
Write-Warning "It may take some time to calculate all host addresses for a /$MaskBits subnet. Use -SkipHosts to skip."
}
$MaskAddr = [Net.IPAddress]::Parse((Convert-INT64toIP -int ([convert]::ToInt64(("1" * $MaskBits + "0" * (32 - $MaskBits)), 2))))
$NetworkAddr = New-Object net.ipaddress ($MaskAddr.address -band $IPAddr.address)
$BroadcastAddr = New-Object net.ipaddress (([system.net.ipaddress]::parse("255.255.255.255").address -bxor $MaskAddr.address -bor $NetworkAddr.address))
$HostStartAddr = (Convert-IPtoINT64 -ip $NetworkAddr.ipaddresstostring) + 1
$HostEndAddr = (Convert-IPtoINT64 -ip $broadcastaddr.ipaddresstostring) - 1
If (-not $SkipHosts) {
$HostAddresses = for ($i = $HostStartAddr; $i -le $HostEndAddr; $i++) {
Convert-INT64toIP -int $i
}
}
[pscustomobject]@{
IPAddress = $IPAddr
MaskBits = $MaskBits
NetworkAddress = $NetworkAddr
BroadcastAddress = $broadcastaddr
SubnetMask = $MaskAddr
NetworkClass = $Class
Range = "$networkaddr ~ $broadcastaddr"
HostAddresses = $HostAddresses
}
}
End {}
}
Get-Subnet -IP 192.168.1.3/21