Advertisement
metalx1000

Basic Network Scanner

Jun 2nd, 2015
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. Basic Network Scanner
  3.  
  4. Example useage:
  5. PowerShell -ExecutionPolicy Bypass .\scanner.ps1 <Base IP> <Port>
  6. PowerShell -ExecutionPolicy Bypass .\scanner.ps1 192.168.20 80
  7. #>
  8.  
  9. $ErrorActionPreference= 'silentlycontinue' #ignore errors
  10.  
  11. #check for user input
  12. if($args[0]){
  13.     $baseIP = $args[0]
  14. }else{
  15.     $baseIP = "192.168.1"
  16. }
  17.  
  18. if($args[1]){
  19.     $port = $args[1]
  20. }else{
  21.     $port = 80
  22. }
  23.  
  24. $rangeIP = 1..254
  25.  
  26. foreach ($range in $rangeIP){
  27.   $ip = "{0}.{1}" -F $baseIP,$range
  28.   write-host "Scanning $ip port $port..." -NoNewline
  29.  
  30.   if(Test-Connection -BufferSize 32 -Count 1 -Quiet -ComputerName $ip){
  31.      $socket = new-object System.Net.Sockets.TcpClient($ip, $port)
  32.          
  33.      If($socket.Connected){
  34.         $socket.Close()
  35.         write-host "`r$ip port $port is open." -foregroundcolor "red" #if port open output
  36.      }
  37.    }
  38.      write-host "`r" -NoNewline #clear line is port not open
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement