module MacAddresses
Constants
- ADDRESS_REGEXP
- COMMANDS
- NOT_VALID_MACS
- PROTOCOL_FAMILIES
- PROTOCOL_FAMILY
- VERSION
Attributes
addresses[R]
list[R]
Public Class Methods
fetch()
click to toggle source
Discovers and returns the system's MAC addresses.
MacAddresses.fetch
# File lib/mac_addresses.rb, line 41 def fetch @addresses = from_getifaddrs return @addresses if @addresses success = false COMMANDS.each do |cmd| stdout = `#{cmd}` rescue nil next unless stdout && stdout.length > 0 @addresses = parse(stdout) success = true break # break as soon as we successfully parse a command output end unless success raise Exceptions::NoneOfCommandsSuccessful, COMMANDS end @addresses end
from_getifaddrs()
click to toggle source
# File lib/mac_addresses.rb, line 61 def from_getifaddrs return nil unless Socket.respond_to? :getifaddrs interfaces = Socket.getifaddrs.select do |addr| addr.addr && # Some VPN ifcs don't have an addr - ignore them addr.addr.pfamily == MacAddresses::PROTOCOL_FAMILY end interfaces.inject([]) do |found, iface| macs = parse iface.addr.inspect_sockaddr found << macs.select { |mac| mac != '00:00:00:00:00:00' } end.flatten end
parse(string)
click to toggle source
Scans a string and returns an Array of found MACs
# File lib/mac_addresses.rb, line 76 def parse(string) string.scan(ADDRESS_REGEXP).flatten end