Method: Phut::Netns.all

Defined in:
lib/phut/netns.rb

.allObject

rubocop:disable MethodLength rubocop:disable AbcSize



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/phut/netns.rb', line 16

def self.all
  sh('ip netns list').split("\n").map do |each|
    name = each.split.first
    ip_addr_list =
      sudo("ip netns exec #{name} ip -4 -o addr list").split("\n")
    mac_addr_list =
      sudo("ip netns exec #{name} ip -4 -o link list").split("\n")
    if ip_addr_list.size > 1
      %r{inet ([^/]+)/(\d+)} =~ ip_addr_list[1]
      ip_address = Regexp.last_match(1)
      mask_length = Regexp.last_match(2).to_i
      netmask = IPAddr.new('255.255.255.255').mask(mask_length).to_s
      %r{link/ether ((?:[a-f\d]{2}:){5}[a-f\d]{2})}i =~ mac_addr_list[1]
      new(name: name, ip_address: ip_address, netmask: netmask,
          mac_address: Regexp.last_match(1))
    else
      new(name: name)
    end
  end.sort_by(&:name)
end