Class: IPScannerPlus

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

Overview

above info from en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers via

http://a0.jamesrobertson.eu/dynarex/ports-wellknown.xml

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(devices: nil, ports: nil) ⇒ IPScannerPlus

Returns a new instance of IPScannerPlus.



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/ipscannerplus.rb', line 230

def initialize(devices: nil, ports: nil)
  
  @custom_ports = ports.strip.lines.map do |x| 
    (x.chomp.split(/ +/,2) + ['']).take(2)
  end.to_h
  
  @known_ports = PORTS.strip.lines.map {|x| x.chomp.split(/ +/,2)}.to_h
  
  @devices = devices.strip.lines.map do |x| 
    a = x.chomp.split(/ +/,3)
    [a[0], a[1..2]]
  end.to_h
  
  @ports = @known_ports.merge(@custom_ports)
  @nameserver = Resolv::DNS::Config.new.lazy_initialize.nameserver_port[0][0]
  
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



228
229
230
# File 'lib/ipscannerplus.rb', line 228

def result
  @result
end

Instance Method Details

#ipscanObject



258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/ipscannerplus.rb', line 258

def ipscan()
  
  @result = IPScanner.scan.map do |ip|
    
    deviceid = @devices[ip[/\d+$/]] || [nslookup(ip)]
    
    [
      [ip, deviceid], 
    ]      
  end
  
end

#lookup(service: nil, port: nil) ⇒ Object



248
249
250
251
252
253
254
255
256
# File 'lib/ipscannerplus.rb', line 248

def lookup(service: nil, port: nil)
  
  if service then
    @result.find {|x| x.last.find {|y| y.last =~ /#{service}/i}} 
  elsif port 
    @result.find {|x| x.last.find {|y| y.first == port.to_i}}
  end
  
end

#scanObject



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/ipscannerplus.rb', line 271

def scan()
  
  @result = IPScanner.scan.map do |ip| 
    
    ports = FastPortScanner.scan(ip, ports: (1..1000).to_a \
                                 + @custom_ports.keys.map(&:to_i))
    
    deviceid = @devices[ip[/\d+$/]] || [nslookup(ip)]
    
    [
      [ip, deviceid], 
       ports.map { |port| [port, @ports[port.to_s]] }
    ]
    
  end

end

#to_sObject



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/ipscannerplus.rb', line 289

def to_s()
  
  lines = @result.map do |line|

    rawheader, body = *line

    #header = "" % rawheader.join(' ')
    header = [
      rawheader[0][/\d+$/].green,
      rawheader[1].to_a[0].to_s.length > 1 ? rawheader[1][0].brown.bold : \
        'unknown'.red,
      rawheader[1] ? rawheader[1][1] : ''
    ].join(' ')

    head = if header.length > 75 then
      header[0..72] + '...'
    else
      header
    end

    if body then
      
      head + "\n\n" + body.map do |x|
        desc = x[1].to_s.length > 38 ? (x[1][0..34] + '...') : x[1]
        colour = (x[0].to_i < 1024) ? :gray : :cyan
        "   %+14s %s" % [x[0].to_s.send(colour), desc]
      end.join("\n") + "\n"
      
    else
      
      head
      
    end

  end

  "%s devices found on subnet %s\n\n%s " % [lines.length.to_s, 
                                            @result[0][0][0][/\d+\.\d+\.\d+/], 
                                            lines.join("\n") ]
  
end