Method: NetworkScanner::Scanner#check_ports

Defined in:
lib/network_scanner.rb

#check_ports(port) ⇒ Object

Raises:

  • (Exception)


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/network_scanner.rb', line 158

def check_ports(port)
  raise Exception.new("Must scan ips first (Specify an interface or cacheread)") unless @ips_to_check

  puts "Checking for ports out of a total of #{@ips_to_check.length} ips"

  if self.output_name
    out = File.open(self.output_name, 'w')
  else
    out = STDOUT
  end

  pool = Thread.pool(@pool_size)

  @ips = []

  @ips_to_check.each do |ip|
    pool.process do
      if port_open?(ip, port)
        @ips << ip
        if text?
          out.print "#{ip}\n"
        end
      end
    end
  end

  pool.shutdown

  if json?
    out.puts(JSON.pretty_generate(@ips))
  end

  out.close unless out == STDOUT

  return @ips
end