lan_scanner 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 06453e92801f03ccff92ba3b1b0155e540ca626228f3b2d61f72f8605c851bac
4
- data.tar.gz: 9a28ca27720d9d69c3bf93cdaf0f9f6b9693bfdee5fc9e20ec2b7f78152b85ff
3
+ metadata.gz: ad9ff972172708d293dd398f7a9632b46442af01c46c872dcc4e0c592e61b9a4
4
+ data.tar.gz: e9f7c3b675dcf5f2c7c64dbca357e7c7b61fcb68fcd98c4ef7a1398d0790988e
5
5
  SHA512:
6
- metadata.gz: '0598fb4619c48ae76d23a19d3b9255b9f679f13f2df5f32ff1768293b2c7ebf368ea34d82fbb94c0787223d05f1edb4908dff8fbe1f4385cbab66aa958b23e66'
7
- data.tar.gz: fef519bb6deb35fe43e99f6b8d6d68151ad5fe724ce105802810ead54ddc926d6eb592be0e0bca842732498839e01b45c6695c998790958dd0eb846f77aa8890
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.2'.freeze
2
+ VERSION = '0.0.3'.freeze
3
3
  end
data/lib/lan_scanner.rb CHANGED
@@ -19,7 +19,7 @@ module LanScanner
19
19
  end
20
20
  network = [network] unless network.is_a? Array
21
21
  sn_xml_results = []
22
- tmp_file = "#{Dir.tmpdir}/nmap_scan.xml"
22
+ tmp_file = "#{Dir.tmpdir}/nmap_scan_#{Random.random_number}.xml"
23
23
  # first we do an -sL scan, which also receives addresses from router/network cache,
24
24
  # that are not found by -sn scan when scanning for the complete network, but are found
25
25
  # with -sn scan, when scanning for this addresses explicitly
@@ -48,6 +48,16 @@ module LanScanner
48
48
  _parse_nmap_xml sn_xml_results
49
49
  end
50
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') }
59
+ end
60
+
51
61
  def self.my_networks
52
62
  my_ip_addresses.map do |a|
53
63
  if a.include?('.')
@@ -88,7 +98,7 @@ module LanScanner
88
98
  results.values.select do |r|
89
99
  r.state == 'up' || r.host_name
90
100
  end.map do |r|
91
- 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
92
102
  end
93
103
  end
94
104
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lan_scanner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthäus J. N. Beyrle