lan_scanner 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b03b665db6f858b32fd78d0fa4349ddb272493f86ee947c03ffa6540ee4f950
4
- data.tar.gz: 719c9f103d2b88365308fd5cea1a67156ba822f34e3ab82f2193d9e86a85b23c
3
+ metadata.gz: ad9ff972172708d293dd398f7a9632b46442af01c46c872dcc4e0c592e61b9a4
4
+ data.tar.gz: e9f7c3b675dcf5f2c7c64dbca357e7c7b61fcb68fcd98c4ef7a1398d0790988e
5
5
  SHA512:
6
- metadata.gz: ff053ab65a6ea8c93586ff9adb74cc0293bca5caf0415074e6a4ae70cb3340a717d0759836515f71e21b39be125d355859dced101d16c4295f2e31b36869167d
7
- data.tar.gz: 3bcf333729f6b09ce19daa4731c86981bb29f741c0d358e83a2f04ea73f7e434247039eb0f7027678d2320b1b8add626d94f45bc34153c22b264d37e9b2e72f0
6
+ metadata.gz: 68d459f8f39d95cbf3b891e860b5716f252f78e13f20d7aaf89a724ee42c207fb6968ceb644fa8fd5e8e3d84c76564db9c905a0069d2c9b544c30f7eb9362d17
7
+ data.tar.gz: 15215140fe414f6e9a0bb11c49132fa00ee9d13c45c419f3bf7afc06b18f14da0a56e8c319198c5a43bbc274d27b6d34172bdf58220c199f896e1c73eed9c121
data/README.md CHANGED
@@ -20,7 +20,7 @@ Based on nmap.
20
20
  <a name="usage"></a>
21
21
  ## Usage
22
22
 
23
- ### Find devices in LAN
23
+ ### Find online devices in LAN
24
24
  ```ruby
25
25
  require 'lan_scanner'
26
26
 
@@ -43,6 +43,21 @@ end
43
43
 
44
44
  ```
45
45
 
46
+ ### Get state of devices in LAN
47
+
48
+ ```ruby
49
+ require 'lan_scanner'
50
+
51
+ devices = LanScanner.scan_device_states %w[192.168.178.1 192.168.178.22 192.168.178.44]
52
+ # => [LanScanner::Device, LanScanner::Device, ...]
53
+
54
+ devices.each do |d|
55
+ puts "#{d.remote_address} -> #{d.host_name} (#{d.state})"
56
+ end
57
+ # 192.168.178.1 -> server.domain (up)
58
+ # 192.168.178.22 -> mycomputer.domain (up)
59
+ # 192.168.178.44 -> (down)
60
+ ```
46
61
 
47
62
  <a name="installation"></a>
48
63
  ## Installation
@@ -3,10 +3,12 @@ module LanScanner
3
3
  class Device
4
4
  attr_reader :host_name
5
5
  attr_reader :remote_address
6
+ attr_reader :state # 'up','down','unknown'
6
7
 
7
- def initialize(remote_address:, host_name: nil)
8
+ def initialize(remote_address:, host_name: nil, state: nil)
8
9
  @host_name = host_name
9
10
  @remote_address = remote_address
11
+ @state = state
10
12
  end
11
13
  end
12
14
 
@@ -1,3 +1,3 @@
1
1
  module LanScanner
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.0.3'.freeze
3
3
  end
data/lib/lan_scanner.rb CHANGED
@@ -18,16 +18,44 @@ module LanScanner
18
18
  network = my_networks
19
19
  end
20
20
  network = [network] unless network.is_a? Array
21
- xml_results = []
22
- tmp_file = "#{Dir.tmpdir}/nmap_scan.xml"
21
+ sn_xml_results = []
22
+ tmp_file = "#{Dir.tmpdir}/nmap_scan_#{Random.random_number}.xml"
23
+ # first we do an -sL scan, which also receives addresses from router/network cache,
24
+ # that are not found by -sn scan when scanning for the complete network, but are found
25
+ # with -sn scan, when scanning for this addresses explicitly
26
+ #
27
+ # so after this scan we scan for this addresses beneath the networks with -sn
28
+ sl_xml_results = []
23
29
  network.each do |n|
24
- ['-sL','-sn'].each do |nmap_type|
30
+ ['-sL'].each do |nmap_type|
25
31
  `nmap #{nmap_type} #{n} -oX "#{tmp_file}"`
26
- xml_results.push File.read tmp_file
32
+ sl_xml_results.push File.read tmp_file
27
33
  File.delete tmp_file
28
34
  end
29
35
  end
30
- _parse_nmap_xml xml_results
36
+ # here we scan for the received ip addresses from network cache
37
+ sl_ips = _parse_nmap_xml sl_xml_results
38
+ `nmap -sn #{sl_ips.map(&:remote_address).join(' ')} -oX "#{tmp_file}"`
39
+ sn_xml_results.push File.read tmp_file
40
+ # here we ping the networks (fast ping which does not detect all)
41
+ network.each do |n|
42
+ ['-sn'].each do |nmap_type|
43
+ `nmap #{nmap_type} #{n} -oX "#{tmp_file}"`
44
+ sn_xml_results.push File.read tmp_file
45
+ File.delete tmp_file
46
+ end
47
+ end
48
+ _parse_nmap_xml sn_xml_results
49
+ end
50
+
51
+ # get states of given addresses
52
+ def self.scan_device_states addresses
53
+ addresses = [addresses] unless addresses.is_a? Array
54
+ tmp_file = "#{Dir.tmpdir}/nmap_scan_#{Random.random_number}.xml"
55
+ `nmap -sn #{addresses.join(' ')} -oX "#{tmp_file}"`
56
+ online_hosts = _parse_nmap_xml [File.read(tmp_file)]
57
+ offline_addresses = addresses.reject { |a| online_hosts.map(&:remote_address).include?(a) }
58
+ online_hosts + offline_addresses.map { |a| OpenStruct.new(remote_address: a, host_name: nil, state: 'down') }
31
59
  end
32
60
 
33
61
  def self.my_networks
@@ -70,7 +98,7 @@ module LanScanner
70
98
  results.values.select do |r|
71
99
  r.state == 'up' || r.host_name
72
100
  end.map do |r|
73
- LanScanner::Device.new host_name: r.host_name, remote_address: r.remote_address
101
+ LanScanner::Device.new host_name: r.host_name, remote_address: r.remote_address, state: r.state
74
102
  end
75
103
  end
76
104
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lan_scanner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthäus J. N. Beyrle
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-21 00:00:00.000000000 Z
11
+ date: 2023-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tty-which