lib/lan_scanner.rb in lan_scanner-0.0.2 vs lib/lan_scanner.rb in lan_scanner-0.0.3
- old
+ new
@@ -17,11 +17,11 @@
if network.nil?
network = my_networks
end
network = [network] unless network.is_a? Array
sn_xml_results = []
- tmp_file = "#{Dir.tmpdir}/nmap_scan.xml"
+ tmp_file = "#{Dir.tmpdir}/nmap_scan_#{Random.random_number}.xml"
# first we do an -sL scan, which also receives addresses from router/network cache,
# that are not found by -sn scan when scanning for the complete network, but are found
# with -sn scan, when scanning for this addresses explicitly
#
# so after this scan we scan for this addresses beneath the networks with -sn
@@ -46,10 +46,20 @@
end
end
_parse_nmap_xml sn_xml_results
end
+ # get states of given addresses
+ def self.scan_device_states addresses
+ addresses = [addresses] unless addresses.is_a? Array
+ tmp_file = "#{Dir.tmpdir}/nmap_scan_#{Random.random_number}.xml"
+ `nmap -sn #{addresses.join(' ')} -oX "#{tmp_file}"`
+ online_hosts = _parse_nmap_xml [File.read(tmp_file)]
+ offline_addresses = addresses.reject { |a| online_hosts.map(&:remote_address).include?(a) }
+ online_hosts + offline_addresses.map { |a| OpenStruct.new(remote_address: a, host_name: nil, state: 'down') }
+ end
+
def self.my_networks
my_ip_addresses.map do |a|
if a.include?('.')
a.split('.')[0..2].join('.') + '.0/24'
else
@@ -86,9 +96,9 @@
end
puts
results.values.select do |r|
r.state == 'up' || r.host_name
end.map do |r|
- LanScanner::Device.new host_name: r.host_name, remote_address: r.remote_address
+ LanScanner::Device.new host_name: r.host_name, remote_address: r.remote_address, state: r.state
end
end
end
\ No newline at end of file