class LinuxAdmin::NetworkInterface

Attributes

interface[R]

@return [String] the interface for networking operations

Public Class Methods

dist_class(clear_cache = false) click to toggle source

Gets the subclass specific to the local Linux distro

@param clear_cache [Boolean] Determines if the cached value will be reevaluated @return [Class] The proper class to be used

# File lib/linux_admin/network_interface.rb, line 13
def self.dist_class(clear_cache = false)
  @dist_class = nil if clear_cache
  @dist_class ||= begin
    if [Distros.rhel, Distros.fedora].include?(Distros.local)
      NetworkInterfaceRH
    else
      NetworkInterfaceGeneric
    end
  end
end
list() click to toggle source
# File lib/linux_admin/network_interface.rb, line 24
def self.list
  ip_link.pluck("ifname").map { |iface| new(iface) }
rescue AwesomeSpawn::CommandResultError => e
  raise NetworkInterfaceError.new(e.message, e.result)
end
new(*args) click to toggle source

Creates an instance of the correct NetworkInterface subclass for the local distro

Calls superclass method
# File lib/linux_admin/network_interface.rb, line 40
def self.new(*args)
  self == LinuxAdmin::NetworkInterface ? dist_class.new(*args) : super
end
new(interface) click to toggle source

@param interface [String] Name of the network interface to manage

# File lib/linux_admin/network_interface.rb, line 48
def initialize(interface)
  @interface = interface
  reload
end

Public Instance Methods

address() click to toggle source

Retrieve the IPv4 address assigned to the interface

@return [String] IPv4 address for the managed interface

# File lib/linux_admin/network_interface.rb, line 86
def address
  @network_conf[:address]
end
address6(scope = :global) click to toggle source

Retrieve the IPv6 address assigned to the interface

@return [String] IPv6 address for the managed interface @raise [ArgumentError] if the given scope is not ‘:global` or `:link`

# File lib/linux_admin/network_interface.rb, line 94
def address6(scope = :global)
  case scope
  when :global
    @network_conf[:address6_global]
  when :link
    @network_conf[:address6_link]
  else
    raise ArgumentError, "Unrecognized address scope #{scope}"
  end
end
gateway() click to toggle source

Retrieve the IPv4 default gateway associated with the interface

@return [String] IPv4 gateway address

# File lib/linux_admin/network_interface.rb, line 152
def gateway
  @network_conf[:gateway4]
end
gateway6() click to toggle source

Retrieve the IPv6 default gateway associated with the interface

@return [String] IPv6 gateway address

# File lib/linux_admin/network_interface.rb, line 159
def gateway6
  @network_conf[:gateway6]
end
loopback?() click to toggle source
# File lib/linux_admin/network_interface.rb, line 79
def loopback?
  @link_type == "loopback"
end
mac_address() click to toggle source

Retrieve the MAC address associated with the interface

@return [String] the MAC address

# File lib/linux_admin/network_interface.rb, line 108
def mac_address
  @network_conf[:mac]
end
netmask() click to toggle source

Retrieve the IPv4 sub-net mask assigned to the interface

@return [String] IPv4 netmask

# File lib/linux_admin/network_interface.rb, line 115
def netmask
  @network_conf[:mask] ||= IPAddr.new('255.255.255.255').mask(prefix).to_s if prefix
end
netmask6(scope = :global) click to toggle source

Retrieve the IPv6 sub-net mask assigned to the interface

@return [String] IPv6 netmask @raise [ArgumentError] if the given scope is not ‘:global` or `:link`

# File lib/linux_admin/network_interface.rb, line 123
def netmask6(scope = :global)
  if [:global, :link].include?(scope)
    @network_conf["mask6_#{scope}".to_sym] ||= IPAddr.new('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff').mask(prefix6(scope)).to_s if prefix6(scope)
  else
    raise ArgumentError, "Unrecognized address scope #{scope}"
  end
end
prefix() click to toggle source

Retrieve the IPv4 sub-net prefix length assigned to the interface

@return [Numeric] IPv4 prefix length

# File lib/linux_admin/network_interface.rb, line 134
def prefix
  @network_conf[:prefix]
end
prefix6(scope = :global) click to toggle source

Retrieve the IPv6 sub-net prefix length assigned to the interface

@return [Numeric] IPv6 prefix length

# File lib/linux_admin/network_interface.rb, line 141
def prefix6(scope = :global)
  if [:global, :link].include?(scope)
    @network_conf["prefix6_#{scope}".to_sym]
  else
    raise ArgumentError, "Unrecognized address scope #{scope}"
  end
end
reload() click to toggle source

Gathers current network information for this interface

@return [Boolean] true if network information was gathered successfully

# File lib/linux_admin/network_interface.rb, line 56
def reload
  @network_conf = {}
  begin
    ip_output = ip_show
  rescue NetworkInterfaceError
    return false
  end

  @link_type = ip_output["link_type"]
  addr_info  = ip_output["addr_info"]

  parse_ip4(addr_info)
  parse_ip6(addr_info, "global")
  parse_ip6(addr_info, "link")

  @network_conf[:mac] = ip_output["address"]

  [4, 6].each do |version|
    @network_conf["gateway#{version}".to_sym] = ip_route(version, "default")&.dig("gateway")
  end
  true
end
start() click to toggle source

Brings up the network interface

@return [Boolean] whether the command succeeded or not

# File lib/linux_admin/network_interface.rb, line 166
def start
  Common.run(Common.cmd("ifup"), :params => [@interface]).success?
end
stop() click to toggle source

Brings down the network interface

@return [Boolean] whether the command succeeded or not

# File lib/linux_admin/network_interface.rb, line 173
def stop
  Common.run(Common.cmd("ifdown"), :params => [@interface]).success?
end

Private Instance Methods

ip_route(version, route = "default") click to toggle source

Runs the command ‘ip -[4/6] route` and returns the output

@param version [Fixnum] Version of IP protocol (4 or 6) @return [String] The command output @raise [NetworkInterfaceError] if the command fails

# File lib/linux_admin/network_interface.rb, line 197
def ip_route(version, route = "default")
  output = Common.run!(Common.cmd("ip"), :params => ["--json", "-#{version}", "route", "show", route]).output
  return {} if output.blank?

  JSON.parse(output).first
rescue AwesomeSpawn::CommandResultError => e
  raise NetworkInterfaceError.new(e.message, e.result)
end
ip_show() click to toggle source

Runs the command ‘ip addr show <interface>`

@return [String] The command output @raise [NetworkInterfaceError] if the command fails

# File lib/linux_admin/network_interface.rb, line 183
def ip_show
  output = Common.run!(Common.cmd("ip"), :params => ["--json", "addr", "show", @interface]).output
  return {} if output.blank?

  JSON.parse(output).first
rescue AwesomeSpawn::CommandResultError => e
  raise NetworkInterfaceError.new(e.message, e.result)
end
parse_ip4(addr_info) click to toggle source

Parses the IPv4 information from the output of ‘ip addr show <device>`

@param ip_output [String] The command output

# File lib/linux_admin/network_interface.rb, line 209
def parse_ip4(addr_info)
  inet = addr_info&.detect { |addr| addr["family"] == "inet" }
  return if inet.nil?

  @network_conf[:address] = inet["local"]
  @network_conf[:prefix]  = inet["prefixlen"]
end
parse_ip6(addr_info, scope) click to toggle source

Parses the IPv6 information from the output of ‘ip addr show <device>`

@param ip_output [String] The command output @param scope [Symbol] The IPv6 scope (either ‘:global` or `:local`)

# File lib/linux_admin/network_interface.rb, line 221
def parse_ip6(addr_info, scope)
  inet6 = addr_info&.detect { |addr| addr["family"] == "inet6" && addr["scope"] == scope }
  return if inet6.nil?

  @network_conf["address6_#{scope}".to_sym] = inet6["local"]
  @network_conf["prefix6_#{scope}".to_sym]  = inet6["prefixlen"]
end