I am writing a script that needs to know what the MAC address of the host computer is.
Does anyone know how to do this?
I don't think there is any Ruby built-in function to retrieve that address; you'll likely have to make a system call to list the value (e.g. ifconfig
on UNIX, ipconfig /all
on Win32) and parse the output as necessary.
Something like this (untested pseudocode):
def mac_address
platform = RUBY_PLATFORM.downcase
output = `#{(platform =~ /win32/) ? 'ipconfig /all' : 'ifconfig'}`
case platform
when /darwin/
$1 if output =~ /en1.*?(([A-F0-9]{2}:){5}[A-F0-9]{2})/im
when /win32/
$1 if output =~ /Physical Address.*?(([A-F0-9]{2}-){5}[A-F0-9]{2})/im
# Cases for other platforms...
else nil
end
end
There is a gem called macaddr
that does this, but basically it's parsing the output of the system's ifconfig
. You can see the thread when it was being developed at http://www.ruby-forum.com/topic/113956
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With