Class: LinuxAdmin::NetworkInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/linux_admin/network_interface.rb

Direct Known Subclasses

NetworkInterfaceGeneric, NetworkInterfaceRH

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface) ⇒ NetworkInterface

Returns a new instance of NetworkInterface.

Parameters:

  • interface (String)

    Name of the network interface to manage



48
49
50
51
# File 'lib/linux_admin/network_interface.rb', line 48

def initialize(interface)
  @interface = interface
  reload
end

Instance Attribute Details

#interfaceString (readonly)

Returns the interface for networking operations.

Returns:

  • (String)

    the interface for networking operations



45
46
47
# File 'lib/linux_admin/network_interface.rb', line 45

def interface
  @interface
end

Returns the interface for networking operations.

Returns:

  • (String)

    the interface for networking operations



45
46
47
# File 'lib/linux_admin/network_interface.rb', line 45

def link_type
  @link_type
end

Class Method Details

.dist_class(clear_cache = false) ⇒ Class

Gets the subclass specific to the local Linux distro

Parameters:

  • clear_cache (Boolean) (defaults to: false)

    Determines if the cached value will be reevaluated

Returns:

  • (Class)

    The proper class to be used



13
14
15
16
17
18
19
20
21
22
# 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

.listObject



24
25
26
27
28
# 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) ⇒ Object

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



40
41
42
# File 'lib/linux_admin/network_interface.rb', line 40

def self.new(*args)
  self == LinuxAdmin::NetworkInterface ? dist_class.new(*args) : super
end

Instance Method Details

#addressString

Retrieve the IPv4 address assigned to the interface

Returns:

  • (String)

    IPv4 address for the managed interface



86
87
88
# File 'lib/linux_admin/network_interface.rb', line 86

def address
  @network_conf[:address]
end

#address6(scope = :global) ⇒ String

Retrieve the IPv6 address assigned to the interface

Returns:

  • (String)

    IPv6 address for the managed interface

Raises:

  • (ArgumentError)

    if the given scope is not ‘:global` or `:link`



94
95
96
97
98
99
100
101
102
103
# 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

#gatewayString

Retrieve the IPv4 default gateway associated with the interface

Returns:

  • (String)

    IPv4 gateway address



152
153
154
# File 'lib/linux_admin/network_interface.rb', line 152

def gateway
  @network_conf[:gateway4]
end

#gateway6String

Retrieve the IPv6 default gateway associated with the interface

Returns:

  • (String)

    IPv6 gateway address



159
160
161
# File 'lib/linux_admin/network_interface.rb', line 159

def gateway6
  @network_conf[:gateway6]
end

#loopback?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/linux_admin/network_interface.rb', line 79

def loopback?
  @link_type == "loopback"
end

#mac_addressString

Retrieve the MAC address associated with the interface

Returns:

  • (String)

    the MAC address



108
109
110
# File 'lib/linux_admin/network_interface.rb', line 108

def mac_address
  @network_conf[:mac]
end

#netmaskString

Retrieve the IPv4 sub-net mask assigned to the interface

Returns:

  • (String)

    IPv4 netmask



115
116
117
# 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) ⇒ String

Retrieve the IPv6 sub-net mask assigned to the interface

Returns:

  • (String)

    IPv6 netmask

Raises:

  • (ArgumentError)

    if the given scope is not ‘:global` or `:link`



123
124
125
126
127
128
129
# 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

#prefixNumeric

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

Returns:

  • (Numeric)

    IPv4 prefix length



134
135
136
# File 'lib/linux_admin/network_interface.rb', line 134

def prefix
  @network_conf[:prefix]
end

#prefix6(scope = :global) ⇒ Numeric

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

Returns:

  • (Numeric)

    IPv6 prefix length



141
142
143
144
145
146
147
# 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

#reloadBoolean

Gathers current network information for this interface

Returns:

  • (Boolean)

    true if network information was gathered successfully



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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

#startBoolean

Brings up the network interface

Returns:

  • (Boolean)

    whether the command succeeded or not



166
167
168
# File 'lib/linux_admin/network_interface.rb', line 166

def start
  Common.run(Common.cmd("ifup"), :params => [@interface]).success?
end

#stopBoolean

Brings down the network interface

Returns:

  • (Boolean)

    whether the command succeeded or not



173
174
175
# File 'lib/linux_admin/network_interface.rb', line 173

def stop
  Common.run(Common.cmd("ifdown"), :params => [@interface]).success?
end