Class: Snackhack2::PortScan

Inherits:
Object
  • Object
show all
Defined in:
lib/snackhack2/portscan.rb

Instance Method Summary collapse

Constructor Details

#initialize(ip) ⇒ PortScan

Returns a new instance of PortScan.



6
7
8
# File 'lib/snackhack2/portscan.rb', line 6

def initialize(ip)
  @ip = ip
end

Instance Method Details

#runObject



10
11
12
13
14
15
# File 'lib/snackhack2/portscan.rb', line 10

def run
  threads = []
  ports = [*1..1000]
  ports.each { |i| threads << Thread.new { tcp(i) } }
  threads.each(&:join)
end

#tcp(i) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/snackhack2/portscan.rb', line 17

def tcp(i)
  open_ports = []
  begin
    Timeout.timeout(1) do
      s = TCPSocket.new(@ip, i)
      s.close
      open_ports << i
    rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
      return false
    end
  rescue Timeout::Error
  end
  return if open_ports.empty?

  open_ports.each do |port|
    puts "#{port} is open"
  end
end