SHOW:
|
|
- or go back to the newest paste.
1 | $ErrorActionPreference = "SilentlyContinue" | |
2 | $input1,$input2 = $null | |
3 | ||
4 | # START | The script asks for an IP, put it in a while for error checking. It only checks if the IP is a valid one. | |
5 | while ($input1 -eq $null) | |
6 | { | |
7 | [ipaddress]$input1 = read-host "Please input the first IP address you would like to scan: " | |
8 | } | |
9 | # END | The script asks for an IP, put it in a while for error checking. It only checks if the IP is a valid one. | |
10 | while ($input2 -eq $null) | |
11 | { | |
12 | [ipaddress]$input2 = read-host "Please input the IP address you want to finish the scan: " | |
13 | } | |
14 | # Grabs each respective input and puts them into their own arrays. | |
15 | [array]$ip1 = $input1 -split ".",0,"simplematch"; [array]$ip2 = $input2 -split ".",0,"simplematch" | |
16 | ||
17 | # Begin madness. The ping sweep is four nested for loops. Each for loop interates for each octet. We call the count for each octet a.b.c.d respectively. | |
18 | # $a starts at the first input.. obviously. The first input should be less or equal to the second input. i.e. 192.x.x.x 192..193.x.x.x | |
19 | for ([int]$a = $ip1[0];($a -le $ip2[0]); $a++) | |
20 | { | |
21 | # Say we're interating through a ton of IPs.. eventually when we get to where we want to be.. we need to reset the limit to stop at our second input | |
22 | # input will never change.. but ip1 and ip2 can because they are our limiters. | |
23 | if ($a -eq $ip2[0]) | |
24 | { | |
25 | [array]$ip2 = $input2 -split ".",0,"simplematch" | |
26 | } | |
27 | ||
28 | # This is the first break.. don't worry about how it's spelled. In order for the loop to break properly.. we had to add a mechanism to exit each nested for. | |
29 | # I.E. when b.c.d. reaches 255.. that was the easiest way to go back to the previous for loop in order to interate to the next subnet. | |
30 | [int]$brakeB = 0 | |
31 | ||
32 | # $b starts at the first inputs second octet. Checks to see if it's less or equal to the second input's second octet OR if $a is less than the input2's first octet. | |
33 | # I.E. 65.77.120.1 , 66.20.50.1. Those are two valid ranges.. but input1 $b > input2 $b. but... 65 < 66.. so we're still good. | |
34 | for ([int]$b = $ip1[1];($b -le $ip2[1] -or $a -lt $ip2[0]) -and [int]$brakeB -eq 0; $b++) | |
35 | { | |
36 | # So.. We have to tell the sweep where to stop when it's iterating.. or.. it'll just keep going. a.b.c.d is our count. ip1 and ip2 are constants/limiters | |
37 | # We want $c to be 255 when we want to add to $b | |
38 | if ($b -lt $ip2[1] -or $a -lt $ip2[0]) | |
39 | { | |
40 | [int]$ip2[2] = 255 | |
41 | } | |
42 | # This checks if $b gets to 255 or 256 and needs to keep going. | |
43 | # First it checks if a is less than ip2's first octet, if it is.. it sets the limit of b to 256, because if it equals 255 it will auto reset to 0 if you start on the 255 subnet | |
44 | if ($a -lt $ip2[0]) | |
45 | { | |
46 | $ip2[1] = 256 | |
47 | # so if $b is 256... that means it wants to keep going.. so reset and break the loop. | |
48 | if ($b -eq $ip2[1]) | |
49 | { | |
50 | $b = 0 | |
51 | $ip1[1] = 0 | |
52 | [int]$brakeB++ | |
53 | } | |
54 | } | |
55 | # This is here to be able to break out of the $c loop | |
56 | $brakeC = 0 | |
57 | ||
58 | # $c starts at input1's third octet. Checks against ip2's a.b.c. If $c less or equal to ip2 OR b < inpute's second octet, or a < input2's first octet. | |
59 | # The breaks make sure that it's supposed to run at that current time | |
60 | for ([int]$c = $ip1[2];($c -le $ip2[2] -or $b -lt $ip2[1] -or $a -lt $ip2[0]) -and $brakeB -eq 0 -and $brakeC -eq 0; $c++) | |
61 | { | |
62 | # if c is less than input2's third octet, it sets the limit to 255 | |
63 | if ($c -lt $ip2[2]) | |
64 | { | |
65 | [int]$ip2[3] = 255 | |
66 | } | |
67 | # if c is equal to input2's third octet, reset the constant so it'll stop if our input told it to | |
68 | elseif ($c -eq $ip2[2]) | |
69 | { | |
70 | [array]$ip2 = $input2 -split ".",0,"simplematch" | |
71 | } | |
72 | # This checks to see if b < input2's second octet or if a < input2's first octet | |
73 | elseif ($b -lt $ip2[1] -or $a -lt $ip2[0]) | |
74 | { | |
75 | # so if $c is 256... that means it wants to keep going.. so reset and break the loop. | |
76 | if ($c -eq 256) | |
77 | { | |
78 | $c = 0 | |
79 | $ip1[2] = 0 | |
80 | $brakeC++ | |
81 | } | |
82 | } | |
83 | # This is here to be able to break out of the $d loop | |
84 | $brakeD = 0 | |
85 | # d starts at input1's fourth octet. checks to see if it's less or equal to it's input2 counterpart | |
86 | # checks to see if a.b.c are less than it's counterpart and if it's supposed to be running | |
87 | for ([int]$d = $ip1[3];($d -le $ip2[3] -or $c -lt $ip2[2] -or $b -lt $ip2[1] -or $a -lt $ip2[0]) -and $BrakeC -eq 0 -and $brakeD -eq 0; $d++) | |
88 | { | |
89 | ping -n 1 "$a.$b.$c.$d" | |
90 | [array]$ip2 = $input2 -split ".",0,"simplematch" | |
91 | if ($c -lt $ip2[2] -or $b -lt $ip2[1] -or $a -lt $ip2[0]) | |
92 | { | |
93 | if ($d -eq 255) | |
94 | { | |
95 | $d = 1 | |
96 | $ip1[3] = 1 | |
97 | $brakeD++ | |
98 | } | |
99 | } | |
100 | } | |
101 | } | |
102 | } | |
103 | } |