Advertisement
metalx1000

Basic Port Scanner

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