mac_addresses 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: 0ece96d157476727f2e33bae51953a10c7ea54341fe22d725ff74a768bcb1f75
4
+ data.tar.gz: faee1e34e0ddd21050254c5b14247e56c36a50a94f6594e39f42c181a12e0d8b
5
+ SHA512:
6
+ metadata.gz: c2070d6473e8936b59e0599b65b21f04ea45625284f23cd9fdc16e426ee22c5106443ba7e48356e40317b54756ed9feba9e959b153f165fb947fd59f3988a2e5
7
+ data.tar.gz: e58ee4bf62eddecec81c5a9cb2cf9cd4f9698a91e45a00eb024664e8cce707b0dff9e245e0792d909ca667a1167e1289d659763030f64c05b24b5bb5b103e1fa
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Fancy Pixel S.r.l. All rights reserved.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a
6
+ copy of this software and associated documentation files (the "Software"),
7
+ to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included
14
+ in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ ![MacAddresses Logo](/assets/logo_no_bg.png)
2
+
3
+ <!-- # [![Build Status](https://travis-ci.org/space-bunny/ruby_sdk.svg)](https://travis-ci.org/space-bunny/ruby_sdk)
4
+ [![Gem Version](https://badge.fury.io/rb/spacebunny.svg)](https://badge.fury.io/rb/spacebunny) -->
5
+
6
+ This Gem has been created with the intention of making it simple to determine MAC addresses.
7
+ Heavily inspired by [macaddr](https://github.com/ahoward/macaddr)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'mac_addresses'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install mac_addresses
24
+
25
+ ## Usage
26
+
27
+ First you have to fetch MAC addresses:
28
+
29
+ ```ruby
30
+ MacAddresses.fetch # this also returns the list of addresses
31
+ ```
32
+
33
+ Then you can access the cached addresses with:
34
+
35
+ ```ruby
36
+ MacAddresses.list
37
+ ```
38
+
39
+ ### Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/FancyPixel/mac_addresses.
42
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere
43
+ to the [Contributor Covenant](contributor-covenant.org) code of conduct.
44
+
45
+ ### Development
46
+
47
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec` to run the tests.
48
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ To install this gem onto your local machine, run `bundle exec rake install`.
51
+
52
+ ### License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,3 @@
1
+ module MacAddresses
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,84 @@
1
+ ##
2
+ # Cross platform MAC address determination. Works for:
3
+ # * /sbin/ifconfig
4
+ # * /bin/ifconfig
5
+ # * ifconfig
6
+ # * ipconfig /all
7
+ #
8
+ # To return an array of all MAC addresses:
9
+ #
10
+ # MacAddresses.fetch
11
+ #
12
+ # To return the fetched addresses without repeating fetch ops again:
13
+ #
14
+ # MacAddresses.list
15
+
16
+ require 'socket'
17
+
18
+ module MacAddresses
19
+
20
+ COMMANDS = '/sbin/ifconfig', '/bin/ifconfig', 'ifconfig', 'ipconfig /all', 'cat /sys/class/net/*/address'
21
+ ADDRESS_REGEXP = %r/(?:[^:\-]|\A)(?:[0-9A-F][0-9A-F][:\-]){5}[0-9A-F][0-9A-F](?:[^:\-]|\Z)/io
22
+ LINK = Socket::PF_LINK if Socket.const_defined? :PF_LINK
23
+ PACKET = Socket::PF_PACKET if Socket.const_defined? :PF_PACKET
24
+ INTERFACE_PACKET_FAMILY = LINK || PACKET
25
+
26
+ class << self
27
+
28
+ attr_reader :addresses
29
+
30
+ alias_method :list, :addresses
31
+
32
+ ##
33
+ # Discovers and returns the system's MAC addresses.
34
+ # MacAddresses.fetch
35
+
36
+ def fetch
37
+ @addresses = from_getifaddrs
38
+ return @addresses if @addresses
39
+
40
+ success = false
41
+ COMMANDS.each do |cmd|
42
+ stdout = `#{cmd}` rescue nil
43
+ next unless stdout && stdout.length > 0
44
+ @addresses = parse(stdout)
45
+ success = true
46
+ break # break as soon as we successfully parse a command output
47
+ end
48
+
49
+ raise "None of #{ COMMANDS.join ', ' } succeeded returning MAC addresses info" unless success
50
+
51
+ @addresses
52
+ end
53
+
54
+ def from_getifaddrs
55
+ return nil unless Socket.respond_to? :getifaddrs
56
+
57
+ interfaces = Socket.getifaddrs.select do |addr|
58
+ addr.addr && # Some VPN ifcs don't have an addr - ignore them
59
+ addr.addr.pfamily == INTERFACE_PACKET_FAMILY
60
+ end
61
+
62
+ macs = if Socket.const_defined? :PF_LINK
63
+ interfaces.map do |addr|
64
+ addr.addr.getnameinfo
65
+ end.map do |m,|
66
+ m if (m && !m.empty?)
67
+ end.compact
68
+ elsif Socket.const_defined? :PF_PACKET
69
+ interfaces.map do |addr|
70
+ addr.addr.inspect_sockaddr[/hwaddr=([\h:]+)/, 1]
71
+ end.map do |mac_addr|
72
+ mac_addr != '00:00:00:00:00:00'
73
+ end
74
+ end
75
+ macs
76
+ end
77
+
78
+ def parse(output)
79
+ candidates = output.scan(ADDRESS_REGEXP).map &:strip
80
+ raise 'No mac address candidates' unless candidates.any?
81
+ candidates
82
+ end
83
+ end
84
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mac_addresses
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alessandro Verlato
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 12.3.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 12.3.3
27
+ description: Cross platform mac addresses determination
28
+ email: alessandro@fancypixel.it
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - LICENSE
34
+ - README.md
35
+ - lib/mac_addresses.rb
36
+ - lib/mac_addresses/version.rb
37
+ homepage: https://github.com/FancyPixel/mac_addresses
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.5.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.0.6
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Cross platform mac addresses determination
60
+ test_files: []