-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpowersweep.ps1
214 lines (146 loc) · 4.6 KB
/
powersweep.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
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
<#
.SYNOPSIS
Powershell ping sweeper
.PARAMETERS
ipaddress : Provide a range of IP address (CIDR Accepted)
#>
# Main function that gets called. Requires a CIDR ip range or "-" seperated start and end IP range
function powersweep(){
[CmdletBinding()]
Param
(
[parameter(mandatory=$true,valuefrompipeline=$true,helpmessage="Example : 192.168.1.1 or 192.168.1.0/24 or 192.168.1.1-192.168.1.20")]
[string]$ipaddress
)
Write-Output "IP address: $ipaddress"
# Detects CIDR
if ($ipaddress -match "\/")
{
Write-Output "CIDR detected"
#splits it up into start and end
$startIP= Get-IPV4NetworkStartIP($ipaddress)
$endIP = Get-IPV4NetworkEndIP($ipaddress)
}
# Detects range
elseif ($ipaddress -match "\-")
{
Write-Output "Range Detected"
#casts to IP address type
$startIP=[System.Net.IPAddress]$ipaddress.Split("-")[0]
$endIP=[System.Net.IPAddress]$ipaddress.Split("-")[1]
}
Write-Output "$startIP : $endIP"
#calls a parallel workflow and searches for lines with ttl
ParallelSweep -startip $startIP -endip $endIP | select-string ttl
}
# Gets the start IP from a CIDR network
Function Get-IPV4NetworkStartIP ($strNetwork)
{
$StrNetworkAddress = ($strNetwork.split("/"))[0]
$NetworkIP = ([System.Net.IPAddress]$StrNetworkAddress).GetAddressBytes()
[Array]::Reverse($NetworkIP)
$NetworkIP = ([System.Net.IPAddress]($NetworkIP -join ".")).Address
$StartIP = $NetworkIP +1
#Convert To Double
If (($StartIP.Gettype()).Name -ine "double")
{
$StartIP = [Convert]::ToDouble($StartIP)
}
$StartIP = [System.Net.IPAddress]$StartIP
Return $StartIP
}
# Gets the end IP from a CIDR network
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
}
# Gets start and end network addresses (not including host octet)
Function Get-network($startip ,$endip)
{
$network_s = @()
$network_e = @()
$startaddr = $startip.IPAddressToString
$endaddr = $endip.IPAddressToString
# $octets_s=@()
# $octets_e=@()
$octets_s = $startaddr.Split("{.}")
$octets_e = $endaddr.Split("{.}")
$startnum = $startaddr.Split("{.}")[3]
$endnum = $endaddr.Split("{.}")[3]
# Write-Output $octets_s
for($i=0;$i -lt 3;$i++){
$network_s += $octets_s[$i]+"."
$network_e += $octets_e[$i]+"."
}
return $network_s ,$network_e
}
# USELESS
# gets a list of subnets
# - not really. Im keeping it incase i realise what i was trying to do
<#
Function Get-subnets($startip,$endip)
{
$startaddr=$startip.IPAddressToString
$endaddr=$endip.IPAddressToString
$out = "False"
$subnets=@()
if($startaddr -le $endaddr){
for($i=0;$i -lt 4;$i++)
{
for($k=0;$k -lt 4;$k++)
{
if($startaddr[$i] -lt $endaddr[$k])
{
$subnets+=$startaddr
}
}
}
}
else
{
$out = "no"
}
return $subnets
}
#>
# Work flow to ping ip addresses
workflow ParallelSweep
{
Param(
[System.Net.IPAddress]$startip,
[System.Net.IPAddress]$endip
)
#$subnets=Get-subnets $startip $endip
#Write-Output $subnets
$network_sub= Get-network -startip $startip -endip $endip
$network_start = $network_sub[0]
$network_end = $network_sub[1]
$endaddr = $endip.IPAddressToString
$endhost = $endaddr.Split("{.}")[3]
#$network = $subnets[0]+$subnets[1]+$subnets[2]
#Write-Output $network
foreach -parallel -throttlelimit 4 ($i in 0..$endhost)
{
$newnet = $network_start+$i -join ""
ping -n 1 -w 100 $newnet
if ($network_start -ne $network_end)
{
$endnet = $network_end+$i -join ""
ping -n 1 -w 100 $endnet
}
}
}
powersweep -ipaddress 192.168.1.0-192.168.1.40