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
-
.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
- .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
49 50 51 |
# File 'lib/macaddr.rb', line 49 def mac_address @mac_address end |
Class Method Details
.address ⇒ Object Also known as: addr
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 |
.dependencies ⇒ Object
32 33 34 35 36 |
# File 'lib/macaddr.rb', line 32 def Mac.dependencies { 'systemu' => [ 'systemu' , '~> 2.6.2' ] } end |
.description ⇒ Object
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 |