powershell v2 working pingsweep tool


SUBMITTED BY: inTHEpast

DATE: April 14, 2017, 3:16 p.m.

FORMAT: Text only

SIZE: 5.3 kB

HITS: 494

  1. $ErrorActionPreference = "SilentlyContinue"
  2. $input1,$input2 = $null
  3. # 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.
  4. while ($input1 -eq $null)
  5. {
  6. [ipaddress]$input1 = read-host "Please input the first IP address you would like to scan: "
  7. }
  8. # 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.
  9. while ($input2 -eq $null)
  10. {
  11. [ipaddress]$input2 = read-host "Please input the IP address you want to finish the scan: "
  12. }
  13. # Grabs each respective input and puts them into their own arrays.
  14. [array]$ip1 = $input1 -split ".",0,"simplematch"; [array]$ip2 = $input2 -split ".",0,"simplematch"
  15. # 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.
  16. # $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
  17. for ([int]$a = $ip1[0];($a -le $ip2[0]); $a++)
  18. {
  19. # 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
  20. # input will never change.. but ip1 and ip2 can because they are our limiters.
  21. if ($a -eq $ip2[0])
  22. {
  23. [array]$ip2 = $input2 -split ".",0,"simplematch"
  24. }
  25. # 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.
  26. # 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.
  27. [int]$brakeB = 0
  28. # $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.
  29. # 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.
  30. for ([int]$b = $ip1[1];($b -le $ip2[1] -or $a -lt $ip2[0]) -and [int]$brakeB -eq 0; $b++)
  31. {
  32. # 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
  33. # We want $c to be 255 when we want to add to $b
  34. if ($b -lt $ip2[1] -or $a -lt $ip2[0])
  35. {
  36. [int]$ip2[2] = 255
  37. }
  38. # This checks if $b gets to 255 or 256 and needs to keep going.
  39. # 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
  40. if ($a -lt $ip2[0])
  41. {
  42. $ip2[1] = 256
  43. # so if $b is 256... that means it wants to keep going.. so reset and break the loop.
  44. if ($b -eq $ip2[1])
  45. {
  46. $b = 0
  47. $ip1[1] = 0
  48. [int]$brakeB++
  49. }
  50. }
  51. # This is here to be able to break out of the $c loop
  52. $brakeC = 0
  53. # $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.
  54. # The breaks make sure that it's supposed to run at that current time
  55. 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++)
  56. {
  57. # if c is less than input2's third octet, it sets the limit to 255
  58. if ($c -lt $ip2[2])
  59. {
  60. [int]$ip2[3] = 255
  61. }
  62. # if c is equal to input2's third octet, reset the constant so it'll stop if our input told it to
  63. elseif ($c -eq $ip2[2])
  64. {
  65. [array]$ip2 = $input2 -split ".",0,"simplematch"
  66. }
  67. # This checks to see if b < input2's second octet or if a < input2's first octet
  68. elseif ($b -lt $ip2[1] -or $a -lt $ip2[0])
  69. {
  70. # so if $c is 256... that means it wants to keep going.. so reset and break the loop.
  71. if ($c -eq 256)
  72. {
  73. $c = 0
  74. $ip1[2] = 0
  75. $brakeC++
  76. }
  77. }
  78. # This is here to be able to break out of the $d loop
  79. $brakeD = 0
  80. # d starts at input1's fourth octet. checks to see if it's less or equal to it's input2 counterpart
  81. # checks to see if a.b.c are less than it's counterpart and if it's supposed to be running
  82. 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++)
  83. {
  84. ping -n 1 "$a.$b.$c.$d"
  85. [array]$ip2 = $input2 -split ".",0,"simplematch"
  86. if ($c -lt $ip2[2] -or $b -lt $ip2[1] -or $a -lt $ip2[0])
  87. {
  88. if ($d -eq 255)
  89. {
  90. $d = 1
  91. $ip1[3] = 1
  92. $brakeD++
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }

comments powered by Disqus