Module: Mac
- Defined in:
- lib/macaddr.rb
Constant Summary collapse
- VERSION =
'1.7.0'
- RE =
%r/(?:[^:\-]|\A)(?:[0-9A-F][0-9A-F][:\-]){5}[0-9A-F][0-9A-F](?:[^:\-]|\Z)/io
Class Attribute Summary collapse
-
.mac_address ⇒ Object
Accessor for the system’s first MAC address, requires a call to #address first.
Class Method Summary collapse
-
.address ⇒ Object
(also: addr)
Discovers and returns the system’s MAC addresses.
- .dependencies ⇒ Object
- .description ⇒ Object
- .from_getifaddrs ⇒ Object
- .parse(output) ⇒ Object
- .version ⇒ Object
Class Attribute Details
.mac_address ⇒ Object
Accessor for the system’s first MAC address, requires a call to #address first
50 51 52 |
# File 'lib/macaddr.rb', line 50 def mac_address @mac_address end |
Class Method Details
.address ⇒ Object Also known as: addr
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/macaddr.rb', line 59 def address return @mac_address if defined? @mac_address and @mac_address @mac_address = from_getifaddrs return @mac_address if @mac_address cmds = '/sbin/ifconfig', '/bin/ifconfig', 'ifconfig', 'ipconfig /all', 'cat /sys/class/net/*/address' output = nil cmds.each do |cmd| _, stdout, _ = systemu(cmd) rescue next next unless stdout and stdout.size > 0 output = stdout and break end raise "all of #{ cmds.join ' ' } failed" unless output @mac_address = parse(output) end |
.dependencies ⇒ Object
33 34 35 36 37 |
# File 'lib/macaddr.rb', line 33 def Mac.dependencies { 'systemu' => [ 'systemu' , '~> 2.6.2' ] } end |
.description ⇒ Object
39 40 41 |
# File 'lib/macaddr.rb', line 39 def Mac.description 'cross platform mac address determination for ruby' end |
.from_getifaddrs ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/macaddr.rb', line 78 def from_getifaddrs return unless Socket.respond_to? :getifaddrs mac, = Socket.getifaddrs.select do |addr| addr.addr.pfamily == Socket::PF_LINK end.map do |addr| addr.addr.getnameinfo end.find do |m,| !m.empty? end @mac_address = mac if mac end |
.parse(output) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/macaddr.rb', line 93 def parse(output) lines = output.split(/\n/) candidates = lines.select{|line| line =~ RE} raise 'no mac address candidates' unless candidates.first candidates.map!{|c| c[RE].strip} maddr = candidates.first raise 'no mac address found' unless maddr maddr.strip! maddr.instance_eval{ @list = candidates; def list() @list end } maddr end |