Class: LinuxAdmin::NetworkInterface

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

Direct Known Subclasses

NetworkInterfaceGeneric, NetworkInterfaceRH

Constant Summary

Constants included from Common

Common::BIN_DIRS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#cmd, #cmd?, #run, #run!

Constructor Details

#initialize(interface) ⇒ NetworkInterface

Returns a new instance of NetworkInterface.

Raises:



37
38
39
40
# File 'lib/linux_admin/network_interface.rb', line 37

def initialize(interface)
  @interface = interface
  reload
end

Instance Attribute Details

#interfaceString (readonly)



33
34
35
# File 'lib/linux_admin/network_interface.rb', line 33

def interface
  @interface
end

Class Method Details

.dist_class(clear_cache = false) ⇒ Class

Gets the subclass specific to the local Linux distro



14
15
16
17
18
19
20
21
22
23
# File 'lib/linux_admin/network_interface.rb', line 14

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

.new(*args) ⇒ Object

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



26
27
28
29
30
# File 'lib/linux_admin/network_interface.rb', line 26

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

Instance Method Details

#addressString

Retrieve the IPv4 address assigned to the interface



66
67
68
# File 'lib/linux_admin/network_interface.rb', line 66

def address
  @network_conf[:address]
end

#address6(scope = :global) ⇒ String

Retrieve the IPv6 address assigned to the interface

Raises:

  • (ArgumentError)

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



74
75
76
77
78
79
80
81
82
83
# File 'lib/linux_admin/network_interface.rb', line 74

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



116
117
118
# File 'lib/linux_admin/network_interface.rb', line 116

def gateway
  @network_conf[:gateway]
end

#mac_addressString

Retrieve the MAC address associated with the interface



88
89
90
# File 'lib/linux_admin/network_interface.rb', line 88

def mac_address
  @network_conf[:mac]
end

#netmaskString

Retrieve the IPv4 sub-net mask assigned to the interface



95
96
97
# File 'lib/linux_admin/network_interface.rb', line 95

def netmask
  @network_conf[:mask]
end

#netmask6(scope = :global) ⇒ String

Retrieve the IPv6 sub-net mask assigned to the interface

Raises:

  • (ArgumentError)

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



103
104
105
106
107
108
109
110
111
# File 'lib/linux_admin/network_interface.rb', line 103

def netmask6(scope = :global)
  if scope == :global
    @network_conf[:mask6_global]
  elsif scope == :link
    @network_conf[:mask6_link]
  else
    raise ArgumentError, "Unrecognized address scope #{scope}"
  end
end

#reloadBoolean

Gathers current network information for this interface

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/linux_admin/network_interface.rb', line 46

def reload
  @network_conf = {}
  return false unless (ip_output = ip_show)

  parse_ip4(ip_output)
  parse_ip6(ip_output, :global)
  parse_ip6(ip_output, :link)

  @network_conf[:mac] = parse_ip_output(ip_output, %r{link/ether}, 1)

  ip_route_res = run!(cmd("ip"), :params => ["route"])
  @network_conf[:gateway] = parse_ip_output(ip_route_res.output, /^default/, 2) if ip_route_res.success?
  true
rescue AwesomeSpawn::CommandResultError => e
  raise NetworkInterfaceError.new(e.message, e.result)
end

#startBoolean

Brings up the network interface



123
124
125
# File 'lib/linux_admin/network_interface.rb', line 123

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

#stopBoolean

Brings down the network interface



130
131
132
# File 'lib/linux_admin/network_interface.rb', line 130

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