class IPScannerPlus
above info from en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers via
http://a0.jamesrobertson.eu/dynarex/ports-wellknown.xml
Attributes
result[R]
Public Class Methods
new(devices: nil, ports: nil)
click to toggle source
# 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
Public Instance Methods
ipscan()
click to toggle source
# 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)
click to toggle source
# 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
scan()
click to toggle source
# 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_s()
click to toggle source
# 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
Private Instance Methods
nslookup(ip)
click to toggle source
# File lib/ipscannerplus.rb, line 333 def nslookup(ip) #`nslookup #{ip}`.strip.lines.last[/(?<=\tname = ).*(?=\.$)/] begin Resolv::DNS.new(:nameserver => [@nameserver]).getname(ip).to_s rescue nil end end