def self.mac_address
if !defined?(@@mac_address)
require 'rbconfig'
os_platform = RbConfig::CONFIG['target_os']
os_class = nil
if (os_platform =~ /win/i && !(os_platform =~ /darwin/i)) ||
os_platform =~ /w32/i
os_class = :windows
elsif os_platform =~ /solaris/i
os_class = :solaris
elsif os_platform =~ /netbsd/i
os_class = :netbsd
elsif os_platform =~ /openbsd/i
os_class = :openbsd
end
mac_regexps = [
Regexp.new("address:? (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
Regexp.new("addr:? (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
Regexp.new("ether:? (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join("-")})")
]
parse_mac = lambda do |output|
(mac_regexps.map do |regexp|
result = output[regexp, 1]
result.downcase.gsub(/-/, ":") if result != nil
end).compact.first
end
if os_class == :windows
script_in_path = true
else
script_in_path = Kernel.system("which ifconfig 2>&1 > /dev/null")
end
if os_class == :solaris
begin
ifconfig_output =
(script_in_path ? `ifconfig -a` : `/sbin/ifconfig -a`)
ip_addresses = ifconfig_output.scan(
/inet\s?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)
ip = ip_addresses.find {|addr| addr[0] != '127.0.0.1'}[0]
@@mac_address = `/usr/sbin/arp #{ip}`.split(' ')[3]
rescue Exception
end
if @@mac_address == "" || @@mac_address == nil
begin
ifconfig_output =
(script_in_path ?
`ifconfig -a` : `/sbin/ifconfig -a`).split(' ')
index = ifconfig_output.index("inet") + 1
ip = ifconfig_output[index]
@@mac_address = `arp #{ip}`.split(' ')[3]
rescue Exception
end
end
elsif os_class == :windows
begin
@@mac_address = parse_mac.call(`ipconfig /all`)
rescue
end
else
begin
if os_class == :netbsd
@@mac_address = parse_mac.call(
script_in_path ? `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`
)
elsif os_class == :openbsd
@@mac_address = parse_mac.call(
script_in_path ? `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`
)
elsif File.exists?('/sbin/ifconfig')
@@mac_address = parse_mac.call(
script_in_path ? `ifconfig 2>&1` : `/sbin/ifconfig 2>&1`
)
if @@mac_address == nil
@@mac_address = parse_mac.call(
script_in_path ?
`ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`
)
end
if @@mac_address == nil
@@mac_address = parse_mac.call(
script_in_path ?
`ifconfig | grep HWaddr | cut -c39- 2>&1` :
`/sbin/ifconfig | grep HWaddr | cut -c39- 2>&1`
)
end
else
@@mac_address = parse_mac.call(
script_in_path ? `ifconfig 2>&1` : `/sbin/ifconfig 2>&1`
)
if @@mac_address == nil
@@mac_address = parse_mac.call(
script_in_path ?
`ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`
)
end
if @@mac_address == nil
@@mac_address = parse_mac.call(
script_in_path ?
`ifconfig | grep HWaddr | cut -c39- 2>&1` :
`/sbin/ifconfig | grep HWaddr | cut -c39- 2>&1`
)
end
end
rescue
end
end
if @@mac_address != nil
if @@mac_address.respond_to?(:to_str)
@@mac_address = @@mac_address.to_str
else
@@mac_address = @@mac_address.to_s
end
@@mac_address.downcase!
@@mac_address.strip!
end
unless @@mac_address.respond_to?(:scan) &&
@@mac_address.scan(/#{(["[0-9a-f]{2}"] * 6).join(":")}/)
@@mac_address = nil
end
end
return @@mac_address
end