lan_scanner 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3b03b665db6f858b32fd78d0fa4349ddb272493f86ee947c03ffa6540ee4f950
4
+ data.tar.gz: 719c9f103d2b88365308fd5cea1a67156ba822f34e3ab82f2193d9e86a85b23c
5
+ SHA512:
6
+ metadata.gz: ff053ab65a6ea8c93586ff9adb74cc0293bca5caf0415074e6a4ae70cb3340a717d0759836515f71e21b39be125d355859dced101d16c4295f2e31b36869167d
7
+ data.tar.gz: 3bcf333729f6b09ce19daa4731c86981bb29f741c0d358e83a2f04ea73f7e434247039eb0f7027678d2320b1b8add626d94f45bc34153c22b264d37e9b2e72f0
data/.gitignore ADDED
@@ -0,0 +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
19
+ /playground.rb
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +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/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lan_scanner.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +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.
data/README.md ADDED
@@ -0,0 +1,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 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
+ <a name="installation"></a>
48
+ ## Installation
49
+
50
+ ### NMAP
51
+
52
+ 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).
53
+
54
+ To check if you have installed nmap correctly, run the following command on a terminal
55
+
56
+ ```
57
+ nmap --version
58
+ ```
59
+
60
+ and you should get some version information. After, you are ready to install the ruby gem.
61
+
62
+ ### Gem
63
+
64
+ Add this line to your application's Gemfile:
65
+
66
+ ```ruby
67
+ gem 'lan_scanner'
68
+ ```
69
+
70
+ And then execute:
71
+
72
+ $ bundle install
73
+
74
+ Or install it yourself by:
75
+
76
+ $ gem install lan_scanner
77
+
78
+
79
+
80
+
81
+
82
+ <a name="documentation"></a>
83
+ ## Documentation
84
+ Check out the doc at RubyDoc
85
+ <a href="https://www.rubydoc.info/gems/lan_scanner">https://www.rubydoc.info/gems/lan_scanner</a>
86
+
87
+
88
+
89
+
90
+
91
+ <a name="contributing"></a>
92
+ ## Contributing
93
+
94
+ 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.
95
+
data/Rakefile ADDED
@@ -0,0 +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
+
15
+ RSpec::Core::RakeTask.new(:spec)
data/bin/console ADDED
@@ -0,0 +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__)
data/bin/setup ADDED
@@ -0,0 +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
@@ -0,0 +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
@@ -0,0 +1,2 @@
1
+ class CustomError < StandardError
2
+ end
@@ -0,0 +1,13 @@
1
+ module LanScanner
2
+
3
+ class Device
4
+ attr_reader :host_name
5
+ attr_reader :remote_address
6
+
7
+ def initialize(remote_address:, host_name: nil)
8
+ @host_name = host_name
9
+ @remote_address = remote_address
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,3 @@
1
+ module LanScanner
2
+ VERSION = '0.0.1'.freeze
3
+ end
@@ -0,0 +1,76 @@
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
+ xml_results = []
22
+ tmp_file = "#{Dir.tmpdir}/nmap_scan.xml"
23
+ network.each do |n|
24
+ ['-sL','-sn'].each do |nmap_type|
25
+ `nmap #{nmap_type} #{n} -oX "#{tmp_file}"`
26
+ xml_results.push File.read tmp_file
27
+ File.delete tmp_file
28
+ end
29
+ end
30
+ _parse_nmap_xml xml_results
31
+ end
32
+
33
+ def self.my_networks
34
+ my_ip_addresses.map do |a|
35
+ if a.include?('.')
36
+ a.split('.')[0..2].join('.') + '.0/24'
37
+ else
38
+ raise "No support for IPv6 devices"
39
+ end
40
+ end
41
+ end
42
+
43
+ def self.my_ip_addresses
44
+ Socket.ip_address_list.select { |ai| ai.ipv4? && !ai.ipv4_loopback? }.map(&:ip_address).uniq
45
+ end
46
+
47
+ private
48
+
49
+ def self._ensure_nmap_available
50
+ unless TTY::Which.exist?("nmap")
51
+ raise "Command 'nmap' not available. Ensure nmap is installed and added to your PATH variable. https://nmap.org'"
52
+ end
53
+ end
54
+
55
+ def self._parse_nmap_xml(xml_contents)
56
+ results = {} # use special hash to avoid duplicates easier
57
+ xml_contents.each do |xml_data|
58
+ xml_obj = Nokogiri::XML(xml_data)
59
+ xml_obj.xpath('//nmaprun/host').each do |host|
60
+ remote_address = host.at('address')['addr']
61
+ host_name = host.at('hostnames/hostname')&.[]('name')
62
+ state = host.at('status')&.[]('state')
63
+ if !results.key?(remote_address) || (results.key?(remote_address) && host_name || state == 'up')
64
+ old_host_name = results[remote_address]&.host_name
65
+ results[remote_address] = OpenStruct.new(remote_address: remote_address, host_name: host_name || old_host_name, state: state)
66
+ end
67
+ end
68
+ end
69
+ puts
70
+ results.values.select do |r|
71
+ r.state == 'up' || r.host_name
72
+ end.map do |r|
73
+ LanScanner::Device.new host_name: r.host_name, remote_address: r.remote_address
74
+ end
75
+ end
76
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lan_scanner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matthäus J. N. Beyrle
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tty-which
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: tmpdir
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.15'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.15'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description:
98
+ email:
99
+ - lan_scanner.gemspec@mail.magynhard.de
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - CODE_OF_CONDUCT.md
107
+ - Gemfile
108
+ - LICENSE
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - lan_sacnner.gemspec
114
+ - lib/custom_errors/custom_error.rb
115
+ - lib/lan_scanner.rb
116
+ - lib/lan_scanner/device.rb
117
+ - lib/lan_scanner/version.rb
118
+ homepage: https://github.com/magynhard/lan_scanner
119
+ licenses:
120
+ - MIT
121
+ metadata:
122
+ allowed_push_host: https://rubygems.org
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '2.3'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubygems_version: 3.1.4
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: The ruby gem to scan your LAN for devices
142
+ test_files: []