lan_scanner 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +18 -18
- data/.rspec +2 -2
- data/CODE_OF_CONDUCT.md +74 -74
- data/Gemfile +4 -4
- data/LICENSE +21 -21
- data/README.md +110 -95
- data/Rakefile +14 -14
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/lan_sacnner.gemspec +41 -41
- data/lib/custom_errors/custom_error.rb +1 -1
- data/lib/lan_scanner/device.rb +14 -12
- data/lib/lan_scanner/version.rb +3 -3
- data/lib/lan_scanner.rb +115 -93
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13967d5ff3f5db3f3da4faaca3350dd6b40021d2ec3bbe748d6c7cd0fe489feb
|
4
|
+
data.tar.gz: da6b68936d13c57e513de23e346765b73de7c5d4bca3baf446c4d2b301efce80
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 819dc36fd94db53195396fdda4863a99745f2a9750dd6cadcd959048054fe9a14b4a5f9655ec609ed92d17dc6ed0f8b7ec39a8b3d0c3397634755b8087e355b1
|
7
|
+
data.tar.gz: 819cb54130e49ef425ed211c5ad606fe5c9fa8462bdc88fd04e519570b2c605a3c7dbf57aa1f1517182a4e60aea8cce2323911ce1018f006f8b328aa8631e77e
|
data/.gitignore
CHANGED
@@ -1,19 +1,19 @@
|
|
1
|
-
/.bundle/
|
2
|
-
/.yardoc
|
3
|
-
/Gemfile.lock
|
4
|
-
/_yardoc/
|
5
|
-
/coverage/
|
6
|
-
/doc/
|
7
|
-
/pkg/
|
8
|
-
/spec/reports/
|
9
|
-
/tmp/
|
10
|
-
/.vscode/
|
11
|
-
/.idea/
|
12
|
-
*.gem
|
13
|
-
|
14
|
-
# rspec failure tracking
|
15
|
-
.rspec_status
|
16
|
-
|
17
|
-
|
18
|
-
/playground
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
10
|
+
/.vscode/
|
11
|
+
/.idea/
|
12
|
+
*.gem
|
13
|
+
|
14
|
+
# rspec failure tracking
|
15
|
+
.rspec_status
|
16
|
+
|
17
|
+
|
18
|
+
/playground
|
19
19
|
/playground.rb
|
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
--format documentation
|
2
|
-
--color
|
1
|
+
--format documentation
|
2
|
+
--color
|
data/CODE_OF_CONDUCT.md
CHANGED
@@ -1,74 +1,74 @@
|
|
1
|
-
# Contributor Covenant Code of Conduct
|
2
|
-
|
3
|
-
## Our Pledge
|
4
|
-
|
5
|
-
In the interest of fostering an open and welcoming environment, we as
|
6
|
-
contributors and maintainers pledge to making participation in our project and
|
7
|
-
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
-
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
-
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
-
orientation.
|
11
|
-
|
12
|
-
## Our Standards
|
13
|
-
|
14
|
-
Examples of behavior that contributes to creating a positive environment
|
15
|
-
include:
|
16
|
-
|
17
|
-
* Using welcoming and inclusive language
|
18
|
-
* Being respectful of differing viewpoints and experiences
|
19
|
-
* Gracefully accepting constructive criticism
|
20
|
-
* Focusing on what is best for the community
|
21
|
-
* Showing empathy towards other community members
|
22
|
-
|
23
|
-
Examples of unacceptable behavior by participants include:
|
24
|
-
|
25
|
-
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
-
advances
|
27
|
-
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
-
* Public or private harassment
|
29
|
-
* Publishing others' private information, such as a physical or electronic
|
30
|
-
address, without explicit permission
|
31
|
-
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
-
professional setting
|
33
|
-
|
34
|
-
## Our Responsibilities
|
35
|
-
|
36
|
-
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
-
behavior and are expected to take appropriate and fair corrective action in
|
38
|
-
response to any instances of unacceptable behavior.
|
39
|
-
|
40
|
-
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
-
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
-
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
-
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
-
threatening, offensive, or harmful.
|
45
|
-
|
46
|
-
## Scope
|
47
|
-
|
48
|
-
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
-
when an individual is representing the project or its community. Examples of
|
50
|
-
representing a project or community include using an official project e-mail
|
51
|
-
address, posting via an official social media account, or acting as an appointed
|
52
|
-
representative at an online or offline event. Representation of a project may be
|
53
|
-
further defined and clarified by project maintainers.
|
54
|
-
|
55
|
-
## Enforcement
|
56
|
-
|
57
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
-
reported by contacting the project team at lan_scanner.gemspec@mail.magynhard.de. All
|
59
|
-
complaints will be reviewed and investigated and will result in a response that
|
60
|
-
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
-
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
-
Further details of specific enforcement policies may be posted separately.
|
63
|
-
|
64
|
-
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
-
faith may face temporary or permanent repercussions as determined by other
|
66
|
-
members of the project's leadership.
|
67
|
-
|
68
|
-
## Attribution
|
69
|
-
|
70
|
-
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
-
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
-
|
73
|
-
[homepage]: http://contributor-covenant.org
|
74
|
-
[version]: http://contributor-covenant.org/version/1/4/
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at lan_scanner.gemspec@mail.magynhard.de. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in lan_scanner.gemspec
|
4
|
-
gemspec
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in lan_scanner.gemspec
|
4
|
+
gemspec
|
data/LICENSE
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
MIT License
|
2
|
-
|
3
|
-
Copyright (c) 2023 Matthäus J. N. Beyrle
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
13
|
-
copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
-
SOFTWARE.
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 Matthäus J. N. Beyrle
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -1,95 +1,110 @@
|
|
1
|
-
# lan_scanner
|
2
|
-
[![Gem](https://img.shields.io/gem/v/lan_scanner?color=default&style=plastic&logo=ruby&logoColor=red)](https://rubygems.org/gems/lan_scanner)
|
3
|
-
![downloads](https://img.shields.io/gem/dt/lan_scanner?color=blue&style=plastic)
|
4
|
-
[![License: MIT](https://img.shields.io/badge/License-MIT-gold.svg?style=plastic&logo=mit)](LICENSE)
|
5
|
-
|
6
|
-
> The very basic ruby gem to scan your LAN for devices
|
7
|
-
|
8
|
-
Based on nmap.
|
9
|
-
|
10
|
-
# Contents
|
11
|
-
|
12
|
-
* [Usage](#usage)
|
13
|
-
* [Installation](#installation)
|
14
|
-
* [Documentation](#documentation)
|
15
|
-
* [Contributing](#contributing)
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
<a name="usage"></a>
|
21
|
-
## Usage
|
22
|
-
|
23
|
-
### Find devices in LAN
|
24
|
-
```ruby
|
25
|
-
require 'lan_scanner'
|
26
|
-
|
27
|
-
# examples with explicit default parameters
|
28
|
-
devices = LanScanner.scan_devices network: '192.168.178.0/24'
|
29
|
-
# => [LanScanner::Device, LanScanner::Device, ...]
|
30
|
-
|
31
|
-
devices.each do |d|
|
32
|
-
puts "=================================================="
|
33
|
-
puts "Found device on #{d.remote_address}"
|
34
|
-
puts
|
35
|
-
puts "Hostname: #{d.host_name}"
|
36
|
-
puts
|
37
|
-
# =================================================="
|
38
|
-
# Found device on 192.168.178.20
|
39
|
-
#
|
40
|
-
# Hostname: Windows-PC
|
41
|
-
#
|
42
|
-
end
|
43
|
-
|
44
|
-
```
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
gem
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
1
|
+
# lan_scanner
|
2
|
+
[![Gem](https://img.shields.io/gem/v/lan_scanner?color=default&style=plastic&logo=ruby&logoColor=red)](https://rubygems.org/gems/lan_scanner)
|
3
|
+
![downloads](https://img.shields.io/gem/dt/lan_scanner?color=blue&style=plastic)
|
4
|
+
[![License: MIT](https://img.shields.io/badge/License-MIT-gold.svg?style=plastic&logo=mit)](LICENSE)
|
5
|
+
|
6
|
+
> The very basic ruby gem to scan your LAN for devices
|
7
|
+
|
8
|
+
Based on nmap.
|
9
|
+
|
10
|
+
# Contents
|
11
|
+
|
12
|
+
* [Usage](#usage)
|
13
|
+
* [Installation](#installation)
|
14
|
+
* [Documentation](#documentation)
|
15
|
+
* [Contributing](#contributing)
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
<a name="usage"></a>
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Find online devices in LAN
|
24
|
+
```ruby
|
25
|
+
require 'lan_scanner'
|
26
|
+
|
27
|
+
# examples with explicit default parameters
|
28
|
+
devices = LanScanner.scan_devices network: '192.168.178.0/24'
|
29
|
+
# => [LanScanner::Device, LanScanner::Device, ...]
|
30
|
+
|
31
|
+
devices.each do |d|
|
32
|
+
puts "=================================================="
|
33
|
+
puts "Found device on #{d.remote_address}"
|
34
|
+
puts
|
35
|
+
puts "Hostname: #{d.host_name}"
|
36
|
+
puts
|
37
|
+
# =================================================="
|
38
|
+
# Found device on 192.168.178.20
|
39
|
+
#
|
40
|
+
# Hostname: Windows-PC
|
41
|
+
#
|
42
|
+
end
|
43
|
+
|
44
|
+
```
|
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], expensive: true
|
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
|
+
```
|
61
|
+
|
62
|
+
<a name="installation"></a>
|
63
|
+
## Installation
|
64
|
+
|
65
|
+
### NMAP
|
66
|
+
|
67
|
+
This gem is based on nmap. So you need to [install nmap](https://nmap.org/download.html) before and ensure it is available via command line (added to PATH environment variable).
|
68
|
+
|
69
|
+
To check if you have installed nmap correctly, run the following command on a terminal
|
70
|
+
|
71
|
+
```
|
72
|
+
nmap --version
|
73
|
+
```
|
74
|
+
|
75
|
+
and you should get some version information. After, you are ready to install the ruby gem.
|
76
|
+
|
77
|
+
### Gem
|
78
|
+
|
79
|
+
Add this line to your application's Gemfile:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
gem 'lan_scanner'
|
83
|
+
```
|
84
|
+
|
85
|
+
And then execute:
|
86
|
+
|
87
|
+
$ bundle install
|
88
|
+
|
89
|
+
Or install it yourself by:
|
90
|
+
|
91
|
+
$ gem install lan_scanner
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
<a name="documentation"></a>
|
98
|
+
## Documentation
|
99
|
+
Check out the doc at RubyDoc
|
100
|
+
<a href="https://www.rubydoc.info/gems/lan_scanner">https://www.rubydoc.info/gems/lan_scanner</a>
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
<a name="contributing"></a>
|
107
|
+
## Contributing
|
108
|
+
|
109
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/magynhard/lan_scanner. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
110
|
+
|
data/Rakefile
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
2
|
-
require "rspec/core/rake_task"
|
3
|
-
|
4
|
-
#
|
5
|
-
# run default task to see tasks to build and publish gem
|
6
|
-
#
|
7
|
-
task :default do
|
8
|
-
system 'rake --tasks'
|
9
|
-
end
|
10
|
-
|
11
|
-
task :test do
|
12
|
-
system 'rspec'
|
13
|
-
end
|
14
|
-
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
#
|
5
|
+
# run default task to see tasks to build and publish gem
|
6
|
+
#
|
7
|
+
task :default do
|
8
|
+
system 'rake --tasks'
|
9
|
+
end
|
10
|
+
|
11
|
+
task :test do
|
12
|
+
system 'rspec'
|
13
|
+
end
|
14
|
+
|
15
15
|
RSpec::Core::RakeTask.new(:spec)
|
data/bin/console
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "lucky_case"
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start(__FILE__)
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "lucky_case"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
#!/usr/bin/env bash
|
2
|
-
set -euo pipefail
|
3
|
-
IFS=$'\n\t'
|
4
|
-
set -vx
|
5
|
-
|
6
|
-
bundle install
|
7
|
-
|
8
|
-
# Do any other automated setup that you need to do here
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
set -euo pipefail
|
3
|
+
IFS=$'\n\t'
|
4
|
+
set -vx
|
5
|
+
|
6
|
+
bundle install
|
7
|
+
|
8
|
+
# Do any other automated setup that you need to do here
|
data/lan_sacnner.gemspec
CHANGED
@@ -1,41 +1,41 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'lan_scanner/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "lan_scanner"
|
8
|
-
spec.version = LanScanner::VERSION
|
9
|
-
spec.authors = ["Matthäus J. N. Beyrle"]
|
10
|
-
spec.email = ["lan_scanner.gemspec@mail.magynhard.de"]
|
11
|
-
|
12
|
-
spec.summary = %q{The ruby gem to scan your LAN for devices}
|
13
|
-
spec.homepage = "https://github.com/magynhard/lan_scanner"
|
14
|
-
spec.license = "MIT"
|
15
|
-
|
16
|
-
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
-
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
-
if spec.respond_to?(:metadata)
|
19
|
-
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
20
|
-
else
|
21
|
-
raise "RubyGems 2.0 or newer is required to protect against " \
|
22
|
-
"public gem pushes."
|
23
|
-
end
|
24
|
-
|
25
|
-
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
|
-
f.match(%r{^(test|spec|features)/})
|
27
|
-
end
|
28
|
-
spec.bindir = 'exe'
|
29
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
-
spec.require_paths = ['lib']
|
31
|
-
|
32
|
-
spec.required_ruby_version = '>= 2.3'
|
33
|
-
|
34
|
-
spec.add_runtime_dependency 'tty-which', '~> 0.5'
|
35
|
-
spec.add_runtime_dependency 'tmpdir', '~> 0.1.3'
|
36
|
-
spec.add_runtime_dependency 'nokogiri', '~> 1.15'
|
37
|
-
|
38
|
-
spec.add_development_dependency 'bundler', '~> 2.0'
|
39
|
-
spec.add_development_dependency 'rake', '>= 10.0'
|
40
|
-
spec.add_development_dependency 'rspec', '>= 3.0'
|
41
|
-
end
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'lan_scanner/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "lan_scanner"
|
8
|
+
spec.version = LanScanner::VERSION
|
9
|
+
spec.authors = ["Matthäus J. N. Beyrle"]
|
10
|
+
spec.email = ["lan_scanner.gemspec@mail.magynhard.de"]
|
11
|
+
|
12
|
+
spec.summary = %q{The ruby gem to scan your LAN for devices}
|
13
|
+
spec.homepage = "https://github.com/magynhard/lan_scanner"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
20
|
+
else
|
21
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
22
|
+
"public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
f.match(%r{^(test|spec|features)/})
|
27
|
+
end
|
28
|
+
spec.bindir = 'exe'
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ['lib']
|
31
|
+
|
32
|
+
spec.required_ruby_version = '>= 2.3'
|
33
|
+
|
34
|
+
spec.add_runtime_dependency 'tty-which', '~> 0.5'
|
35
|
+
spec.add_runtime_dependency 'tmpdir', '~> 0.1.3'
|
36
|
+
spec.add_runtime_dependency 'nokogiri', '~> 1.15'
|
37
|
+
|
38
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
39
|
+
spec.add_development_dependency 'rake', '>= 10.0'
|
40
|
+
spec.add_development_dependency 'rspec', '>= 3.0'
|
41
|
+
end
|
@@ -1,2 +1,2 @@
|
|
1
|
-
class CustomError < StandardError
|
1
|
+
class CustomError < StandardError
|
2
2
|
end
|
data/lib/lan_scanner/device.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
|
-
module LanScanner
|
2
|
-
|
3
|
-
class Device
|
4
|
-
attr_reader :host_name
|
5
|
-
attr_reader :remote_address
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
@
|
10
|
-
|
11
|
-
|
12
|
-
|
1
|
+
module LanScanner
|
2
|
+
|
3
|
+
class Device
|
4
|
+
attr_reader :host_name
|
5
|
+
attr_reader :remote_address
|
6
|
+
attr_reader :state # 'up','down','unknown'
|
7
|
+
|
8
|
+
def initialize(remote_address:, host_name: nil, state: nil)
|
9
|
+
@host_name = host_name
|
10
|
+
@remote_address = remote_address
|
11
|
+
@state = state
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
13
15
|
end
|
data/lib/lan_scanner/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module LanScanner
|
2
|
-
VERSION = '0.0.
|
3
|
-
end
|
1
|
+
module LanScanner
|
2
|
+
VERSION = '0.0.4'.freeze
|
3
|
+
end
|
data/lib/lan_scanner.rb
CHANGED
@@ -1,94 +1,116 @@
|
|
1
|
-
|
2
|
-
require 'socket'
|
3
|
-
require 'ostruct'
|
4
|
-
require 'tty-which'
|
5
|
-
require 'tmpdir'
|
6
|
-
require 'nokogiri'
|
7
|
-
|
8
|
-
|
9
|
-
require_relative 'lan_scanner/device'
|
10
|
-
require_relative 'lan_scanner/version'
|
11
|
-
|
12
|
-
module LanScanner
|
13
|
-
|
14
|
-
# @return [Array<LanScanner::Device>] list of devices
|
15
|
-
def self.scan_devices(network: nil)
|
16
|
-
_ensure_nmap_available
|
17
|
-
if network.nil?
|
18
|
-
network = my_networks
|
19
|
-
end
|
20
|
-
network = [network] unless network.is_a? Array
|
21
|
-
sn_xml_results = []
|
22
|
-
tmp_file = "#{Dir.tmpdir}/
|
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 = []
|
29
|
-
network.each do |n|
|
30
|
-
['-sL'].each do |nmap_type|
|
31
|
-
`nmap #{nmap_type} #{n} -oX "#{tmp_file}"`
|
32
|
-
sl_xml_results.push File.read tmp_file
|
33
|
-
File.delete tmp_file
|
34
|
-
end
|
35
|
-
end
|
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
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
def self.
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
end
|
93
|
-
end
|
1
|
+
|
2
|
+
require 'socket'
|
3
|
+
require 'ostruct'
|
4
|
+
require 'tty-which'
|
5
|
+
require 'tmpdir'
|
6
|
+
require 'nokogiri'
|
7
|
+
|
8
|
+
|
9
|
+
require_relative 'lan_scanner/device'
|
10
|
+
require_relative 'lan_scanner/version'
|
11
|
+
|
12
|
+
module LanScanner
|
13
|
+
|
14
|
+
# @return [Array<LanScanner::Device>] list of devices
|
15
|
+
def self.scan_devices(network: nil)
|
16
|
+
_ensure_nmap_available
|
17
|
+
if network.nil?
|
18
|
+
network = my_networks
|
19
|
+
end
|
20
|
+
network = [network] unless network.is_a? Array
|
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 = []
|
29
|
+
network.each do |n|
|
30
|
+
['-sL'].each do |nmap_type|
|
31
|
+
`nmap #{nmap_type} #{n} -oX "#{tmp_file}"`
|
32
|
+
sl_xml_results.push File.read tmp_file
|
33
|
+
File.delete tmp_file
|
34
|
+
end
|
35
|
+
end
|
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
|
+
# @param [Boolean] expensive make expensive check for devices which were not found by fast check already
|
53
|
+
def self.scan_device_states addresses, expensive: false
|
54
|
+
addresses = [addresses] unless addresses.is_a? Array
|
55
|
+
tmp_file = "#{Dir.tmpdir}/nmap_scan_#{Random.random_number}.xml"
|
56
|
+
nmap_scan_option = if expensive
|
57
|
+
'-Pn'
|
58
|
+
else
|
59
|
+
'-sn'
|
60
|
+
end
|
61
|
+
`nmap -sn #{addresses.join(' ')} -oX "#{tmp_file}"`
|
62
|
+
online_hosts = _parse_nmap_xml [File.read(tmp_file)]
|
63
|
+
offline_addresses = addresses.reject { |a| online_hosts.map(&:remote_address).include?(a) }
|
64
|
+
# check offline addresses again with expensive check
|
65
|
+
if expensive
|
66
|
+
`nmap -Pn #{offline_addresses.join(' ')} -oX "#{tmp_file}"`
|
67
|
+
online_hosts += _parse_nmap_xml [File.read(tmp_file)]
|
68
|
+
offline_addresses = addresses.reject { |a| online_hosts.map(&:remote_address).include?(a) }
|
69
|
+
end
|
70
|
+
online_hosts + offline_addresses.map { |a| OpenStruct.new(remote_address: a, host_name: nil, state: 'down') }
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.my_networks
|
74
|
+
my_ip_addresses.map do |a|
|
75
|
+
if a.include?('.')
|
76
|
+
a.split('.')[0..2].join('.') + '.0/24'
|
77
|
+
else
|
78
|
+
raise "No support for IPv6 devices"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.my_ip_addresses
|
84
|
+
Socket.ip_address_list.select { |ai| ai.ipv4? && !ai.ipv4_loopback? }.map(&:ip_address).uniq
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def self._ensure_nmap_available
|
90
|
+
unless TTY::Which.exist?("nmap")
|
91
|
+
raise "Command 'nmap' not available. Ensure nmap is installed and added to your PATH variable. https://nmap.org'"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def self._parse_nmap_xml(xml_contents)
|
96
|
+
results = {} # use special hash to avoid duplicates easier
|
97
|
+
xml_contents.each do |xml_data|
|
98
|
+
xml_obj = Nokogiri::XML(xml_data)
|
99
|
+
xml_obj.xpath('//nmaprun/host').each do |host|
|
100
|
+
remote_address = host.at('address')['addr']
|
101
|
+
host_name = host.at('hostnames/hostname')&.[]('name')
|
102
|
+
state = host.at('status')&.[]('state')
|
103
|
+
if !results.key?(remote_address) || (results.key?(remote_address) && host_name || state == 'up')
|
104
|
+
old_host_name = results[remote_address]&.host_name
|
105
|
+
results[remote_address] = OpenStruct.new(remote_address: remote_address, host_name: host_name || old_host_name, state: state)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
puts
|
110
|
+
results.values.select do |r|
|
111
|
+
r.state == 'up' || r.host_name
|
112
|
+
end.map do |r|
|
113
|
+
LanScanner::Device.new host_name: r.host_name, remote_address: r.remote_address, state: r.state
|
114
|
+
end
|
115
|
+
end
|
94
116
|
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.
|
4
|
+
version: 0.0.4
|
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:
|
11
|
+
date: 2024-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tty-which
|
@@ -135,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
135
|
- !ruby/object:Gem::Version
|
136
136
|
version: '0'
|
137
137
|
requirements: []
|
138
|
-
rubygems_version: 3.
|
138
|
+
rubygems_version: 3.4.16
|
139
139
|
signing_key:
|
140
140
|
specification_version: 4
|
141
141
|
summary: The ruby gem to scan your LAN for devices
|