SHARE
TWEET

Basic Ruby Port Scanner

a guest Apr 10th, 2016 191 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/ruby
  2.  
  3. require 'socket'
  4. require 'timeout'
  5.  
  6. $rhost = ARGV[0]
  7. $min_port = ARGV[1]
  8. $max_port = ARGV[2]
  9.  
  10. begin
  11.     if (Integer $min_port) <= (Integer $max_port)
  12.         $to_scan = ((Integer $min_port)..(Integer $max_port)).to_a
  13.     else
  14.         puts "[!] Error: Invalid Range of Ports"
  15.         exit
  16.     end
  17. rescue ArgumentError
  18.     puts "[!] Error: Invalid Range of Ports"
  19.     exit
  20. end
  21.  
  22. def scanport(port)
  23.     s = Socket.new Socket::AF_INET, Socket::SOCK_STREAM
  24.     begin
  25.         sockaddr = Socket.pack_sockaddr_in(port, $rhost)
  26.     rescue
  27.         puts "[!] Error: Failed to Resolve Target"
  28.         exit
  29.     end
  30.     timeout(10) do
  31.         begin
  32.             @result = s.connect(sockaddr)
  33.         rescue
  34.             return false
  35.         end
  36.     end
  37.     if @result == 0
  38.         return true
  39.     else
  40.         return false
  41.     end
  42. end
  43.  
  44. puts "[*] Beginning Scan... \n\n"
  45.  
  46. $to_scan.each do |port|
  47.     if scanport(port)
  48.         puts "Port " + port.to_s + ": Open"
  49.     end
  50. end
  51.  
  52. puts "\n[*] Scan Complete!"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top