module Mac
Constants
- RE
- VERSION
Attributes
Accessor for the system’s first MAC address, requires a call to address first
Public Class Methods
Source
# File lib/macaddr.rb, line 59 def address return @mac_address if defined? @mac_address and @mac_address @mac_address = from_getifaddrs return @mac_address if @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
Discovers and returns the system’s MAC addresses. Returns the first MAC address, and includes an accessor list for the remaining addresses:
Mac.addr # => first address Mac.addr.list # => all addresses
Also aliased as: addr
Source
# File lib/macaddr.rb, line 33 def Mac.dependencies { 'systemu' => [ 'systemu' , '~> 2.6.5' ] } end
Source
# File lib/macaddr.rb, line 39 def Mac.description 'cross platform mac address determination for ruby' end
Source
# File lib/macaddr.rb, line 82 def from_getifaddrs return unless Socket.respond_to? :getifaddrs interfaces = Socket.getifaddrs.select do |addr| if addr.addr # Some VPN ifcs don't have an addr - ignore them addr.addr.pfamily == INTERFACE_PACKET_FAMILY end end mac, = if Socket.const_defined? :PF_LINK then interfaces.map do |addr| addr.addr.getnameinfo end.find do |m,| !m.empty? end elsif Socket.const_defined? :PF_PACKET then interfaces.map do |addr| addr.addr.inspect_sockaddr[/hwaddr=([\h:]+)/, 1] end.find do |mac_addr| mac_addr != '00:00:00:00:00:00' end end @mac_address = mac if mac end
Source
# File lib/macaddr.rb, line 109 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