require 'socket'
# バイナリString(6octet)で返す
def if_hwaddr(if_name)
sockaddr = Socket.getifaddrs
.select{|a| a.name == if_name.to_s}
.map(&:addr)
.find{|a| a.pfamily == Socket::PF_PACKET}
.to_sockaddr
sockaddr[-6..-1]
end
# 文字列(16進、":"なし)で返す
def if_hwaddr_str(if_name)
if_hwaddr(if_name).unpack('H*')[0]
end
if_hwaddr_str('eth0') #=> "382e4eb312cd"