Module: Mac
- Defined in:
- lib/macaddr.rb
Constant Summary collapse
- VERSION =
'1.6.1'
- 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
- .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
45 46 47 |
# File 'lib/macaddr.rb', line 45 def mac_address @mac_address end |
Class Method Details
.address ⇒ Object Also known as: addr
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/macaddr.rb', line 54 def address return @mac_address if defined? @mac_address and @mac_address re = %r/[^:\-](?:[0-9A-F][0-9A-F][:\-]){5}[0-9A-F][0-9A-F][^:\-]/io cmds = '/sbin/ifconfig', '/bin/ifconfig', 'ifconfig', 'ipconfig /all', 'cat /sys/class/net/*/address' null = test(?e, '/dev/null') ? '/dev/null' : 'NUL' output = nil cmds.each do |cmd| status, stdout, stderr = 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.5.0' ] } end |
.parse(output) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/macaddr.rb', line 72 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 |