Class: SearchMacAddress::AddrMac

Inherits:
Object
  • Object
show all
Defined in:
lib/search_mac_address/addr_mac.rb

Constant Summary collapse

PHYSICAL_INTERFACE_PATTERN =
/\A(en|eth|ens|eno|enp|wlan|wlp|wifi|wl|lan)\w*/i.freeze
VIRTUAL_INTERFACE_PATTERN =
/\A(lo|loopback|docker|br-|veth|virbr|vmnet|utun|tun|tap|awdl|llw|anpi|gif|stf|bridge|ap)\w*/i.freeze

Class Method Summary collapse

Class Method Details

.by_popenObject



38
39
40
41
42
43
44
# File 'lib/search_mac_address/addr_mac.rb', line 38

def by_popen
  ordered_unique_ips(parse_output(command_output('ifconfig'))).tap do |addresses|
    return addresses if addresses.any?
  end

  ordered_unique_ips(parse_output(command_output('ipconfig /all')))
end

.by_socketObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/search_mac_address/addr_mac.rb', line 22

def by_socket
  interfaces = Socket.getifaddrs.filter_map do |ifaddr|
    next unless usable_ip_interface?(ifaddr)

    {
      name: ifaddr.name.to_s,
      ip: normalized_ip(ifaddr.addr.ip_address),
      family: ifaddr.addr.ipv4? ? :ipv4 : :ipv6
    }
  end

  ordered_unique_ips(interfaces)
rescue StandardError
  []
end

.get_ip_addressesObject



14
15
16
17
18
19
20
# File 'lib/search_mac_address/addr_mac.rb', line 14

def get_ip_addresses
  by_socket.then do |addresses|
    return addresses if addresses.any?
  end

  by_popen
end

.get_physical_addressObject



10
11
12
# File 'lib/search_mac_address/addr_mac.rb', line 10

def get_physical_address
  get_ip_addresses
end