Module: Mac

Defined in:
lib/macaddr.rb

Constant Summary collapse

VERSION =
'1.6.7'
RE =
%r/(?:[^:\-]|\A)(?:[0-9A-F][0-9A-F][:\-]){5}[0-9A-F][0-9A-F](?:[^:\-]|\Z)/io

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.mac_addressObject

Accessor for the system’s first MAC address, requires a call to #address first



49
50
51
# File 'lib/macaddr.rb', line 49

def mac_address
  @mac_address
end

Class Method Details

.addressObject Also known as: addr

Discovers and returns the system’s MAC addresses. Returns the first MAC address, and includes an accessor #list for the remaining addresses:

Mac.addr # => first address
Mac.addr.list # => all addresses


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/macaddr.rb', line 58

def address
  return @mac_address if defined? @mac_address and @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

.dependenciesObject



32
33
34
35
36
# File 'lib/macaddr.rb', line 32

def Mac.dependencies
  {
    'systemu' => [ 'systemu' , '~> 2.6.2' ]
  }
end

.descriptionObject



38
39
40
# File 'lib/macaddr.rb', line 38

def Mac.description
  'cross platform mac address determination for ruby'
end

.parse(output) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/macaddr.rb', line 73

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

.versionObject



28
29
30
# File 'lib/macaddr.rb', line 28

def Mac.version
  ::Mac::VERSION
end