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 +4 -4
- data/README.md +16 -1
- data/lib/lan_scanner/device.rb +3 -1
- data/lib/lan_scanner/version.rb +1 -1
- data/lib/lan_scanner.rb +12 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad9ff972172708d293dd398f7a9632b46442af01c46c872dcc4e0c592e61b9a4
|
4
|
+
data.tar.gz: e9f7c3b675dcf5f2c7c64dbca357e7c7b61fcb68fcd98c4ef7a1398d0790988e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/lan_scanner/device.rb
CHANGED
@@ -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
|
|
data/lib/lan_scanner/version.rb
CHANGED
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}/
|
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
|