mac_addresses 0.0.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mac_addresses.rb +21 -25
- data/lib/mac_addresses/exceptions.rb +18 -0
- data/lib/mac_addresses/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 176bda0f3853e9afa4bb6ee460b2022ad4a0802ca8b9e98b9b373fe4e3ec63af
|
4
|
+
data.tar.gz: 432de641cc9582674578844391c15174e6ed7ef7be46e1494ab9c240f797ca31
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1def1d0cebcd8e2fc833975b6fc06568f48fc653f0cc3247ab3ff29e62d199a54128bb575f4b3d3c6235ef1abc266dda47ed02a841dc32fcf4c3b32511fb8090
|
7
|
+
data.tar.gz: 78cf3ce777df188d59994e581bf937db87bccb13215764f2dc4b258659688e85147a681296c4135120a5adcce8067501611f8dd0880394a5c077edf88aaaadde
|
data/lib/mac_addresses.rb
CHANGED
@@ -14,14 +14,20 @@
|
|
14
14
|
# MacAddresses.list
|
15
15
|
|
16
16
|
require 'socket'
|
17
|
+
require_relative 'mac_addresses/exceptions'
|
17
18
|
|
18
19
|
module MacAddresses
|
19
20
|
|
20
21
|
COMMANDS = '/sbin/ifconfig', '/bin/ifconfig', 'ifconfig', 'ipconfig /all', 'cat /sys/class/net/*/address'
|
21
|
-
ADDRESS_REGEXP =
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
ADDRESS_REGEXP = /([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})/
|
23
|
+
NOT_VALID_MACS = ['00:00:00:00:00:00']
|
24
|
+
PROTOCOL_FAMILIES = 'PF_LINK', 'PF_PACKET'
|
25
|
+
fam = PROTOCOL_FAMILIES.find { |fam| Socket.const_defined?(fam) }
|
26
|
+
if fam
|
27
|
+
PROTOCOL_FAMILY = Socket.const_get fam
|
28
|
+
else
|
29
|
+
raise Exceptions::ProtocolFamilyNotFound, PROTOCOL_FAMILIES
|
30
|
+
end
|
25
31
|
|
26
32
|
class << self
|
27
33
|
|
@@ -32,7 +38,6 @@ module MacAddresses
|
|
32
38
|
##
|
33
39
|
# Discovers and returns the system's MAC addresses.
|
34
40
|
# MacAddresses.fetch
|
35
|
-
|
36
41
|
def fetch
|
37
42
|
@addresses = from_getifaddrs
|
38
43
|
return @addresses if @addresses
|
@@ -46,7 +51,9 @@ module MacAddresses
|
|
46
51
|
break # break as soon as we successfully parse a command output
|
47
52
|
end
|
48
53
|
|
49
|
-
|
54
|
+
unless success
|
55
|
+
raise Exceptions::NoneOfCommandsSuccessful, COMMANDS
|
56
|
+
end
|
50
57
|
|
51
58
|
@addresses
|
52
59
|
end
|
@@ -56,29 +63,18 @@ module MacAddresses
|
|
56
63
|
|
57
64
|
interfaces = Socket.getifaddrs.select do |addr|
|
58
65
|
addr.addr && # Some VPN ifcs don't have an addr - ignore them
|
59
|
-
addr.addr.pfamily ==
|
66
|
+
addr.addr.pfamily == MacAddresses::PROTOCOL_FAMILY
|
60
67
|
end
|
61
68
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
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
|
69
|
+
interfaces.inject([]) do |found, iface|
|
70
|
+
macs = parse iface.addr.inspect_sockaddr
|
71
|
+
found << macs.select { |mac| mac != '00:00:00:00:00:00' }
|
72
|
+
end.flatten
|
76
73
|
end
|
77
74
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
candidates
|
75
|
+
# Scans a string and returns an Array of found MACs
|
76
|
+
def parse(string)
|
77
|
+
string.scan(ADDRESS_REGEXP).flatten
|
82
78
|
end
|
83
79
|
end
|
84
80
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module MacAddresses
|
2
|
+
module Exceptions
|
3
|
+
|
4
|
+
class NoneOfCommandsSuccessful < StandardError
|
5
|
+
def initialize(commands, message = nil)
|
6
|
+
message = message || "None of #{ commands.join(', ') } succeeded returning MAC addresses info"
|
7
|
+
super(message)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class ProtocolFamilyNotFound < StandardError
|
12
|
+
def initialize(families, msg = nil)
|
13
|
+
message = message || "Socket protocol family not found: tried with #{families.join ', '}"
|
14
|
+
super(message)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mac_addresses
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alessandro Verlato
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -33,6 +33,7 @@ files:
|
|
33
33
|
- LICENSE
|
34
34
|
- README.md
|
35
35
|
- lib/mac_addresses.rb
|
36
|
+
- lib/mac_addresses/exceptions.rb
|
36
37
|
- lib/mac_addresses/version.rb
|
37
38
|
homepage: https://github.com/FancyPixel/mac_addresses
|
38
39
|
licenses:
|