Class: LinuxAdmin::NetworkInterface
- Inherits:
-
Object
- Object
- LinuxAdmin::NetworkInterface
- Defined in:
- lib/linux_admin/network_interface.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#interface ⇒ String
readonly
The interface for networking operations.
Class Method Summary collapse
-
.dist_class(clear_cache = false) ⇒ Class
Gets the subclass specific to the local Linux distro.
-
.new(*args) ⇒ Object
Creates an instance of the correct NetworkInterface subclass for the local distro.
Instance Method Summary collapse
-
#address ⇒ String
Retrieve the IPv4 address assigned to the interface.
-
#address6(scope = :global) ⇒ String
Retrieve the IPv6 address assigned to the interface.
-
#gateway ⇒ String
Retrieve the IPv4 default gateway associated with the interface.
-
#gateway6 ⇒ String
Retrieve the IPv6 default gateway associated with the interface.
-
#initialize(interface) ⇒ NetworkInterface
constructor
A new instance of NetworkInterface.
-
#mac_address ⇒ String
Retrieve the MAC address associated with the interface.
-
#netmask ⇒ String
Retrieve the IPv4 sub-net mask assigned to the interface.
-
#netmask6(scope = :global) ⇒ String
Retrieve the IPv6 sub-net mask assigned to the interface.
-
#prefix ⇒ Numeric
Retrieve the IPv4 sub-net prefix length assigned to the interface.
-
#prefix6(scope = :global) ⇒ Numeric
Retrieve the IPv6 sub-net prefix length assigned to the interface.
-
#reload ⇒ Boolean
Gathers current network information for this interface.
-
#start ⇒ Boolean
Brings up the network interface.
-
#stop ⇒ Boolean
Brings down the network interface.
Constructor Details
#initialize(interface) ⇒ NetworkInterface
Returns a new instance of NetworkInterface.
32 33 34 35 |
# File 'lib/linux_admin/network_interface.rb', line 32 def initialize(interface) @interface = interface reload end |
Instance Attribute Details
#interface ⇒ String (readonly)
Returns the interface for networking operations.
29 30 31 |
# File 'lib/linux_admin/network_interface.rb', line 29 def interface @interface end |
Class Method Details
.dist_class(clear_cache = false) ⇒ Class
Gets the subclass specific to the local Linux distro
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/linux_admin/network_interface.rb', line 12 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
24 25 26 |
# File 'lib/linux_admin/network_interface.rb', line 24 def self.new(*args) self == LinuxAdmin::NetworkInterface ? dist_class.new(*args) : super end |
Instance Method Details
#address ⇒ String
Retrieve the IPv4 address assigned to the interface
63 64 65 |
# File 'lib/linux_admin/network_interface.rb', line 63 def address @network_conf[:address] end |
#address6(scope = :global) ⇒ String
Retrieve the IPv6 address assigned to the interface
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/linux_admin/network_interface.rb', line 71 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 ⇒ String
Retrieve the IPv4 default gateway associated with the interface
129 130 131 |
# File 'lib/linux_admin/network_interface.rb', line 129 def gateway @network_conf[:gateway4] end |
#gateway6 ⇒ String
Retrieve the IPv6 default gateway associated with the interface
136 137 138 |
# File 'lib/linux_admin/network_interface.rb', line 136 def gateway6 @network_conf[:gateway6] end |
#mac_address ⇒ String
Retrieve the MAC address associated with the interface
85 86 87 |
# File 'lib/linux_admin/network_interface.rb', line 85 def mac_address @network_conf[:mac] end |
#netmask ⇒ String
Retrieve the IPv4 sub-net mask assigned to the interface
92 93 94 |
# File 'lib/linux_admin/network_interface.rb', line 92 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
100 101 102 103 104 105 106 |
# File 'lib/linux_admin/network_interface.rb', line 100 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 ⇒ Numeric
Retrieve the IPv4 sub-net prefix length assigned to the interface
111 112 113 |
# File 'lib/linux_admin/network_interface.rb', line 111 def prefix @network_conf[:prefix] end |
#prefix6(scope = :global) ⇒ Numeric
Retrieve the IPv6 sub-net prefix length assigned to the interface
118 119 120 121 122 123 124 |
# File 'lib/linux_admin/network_interface.rb', line 118 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 ⇒ Boolean
Gathers current network information for this interface
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/linux_admin/network_interface.rb', line 40 def reload @network_conf = {} begin ip_output = ip_show rescue NetworkInterfaceError return false end 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) [4, 6].each do |version| @network_conf["gateway#{version}".to_sym] = parse_ip_output(ip_route(version), /^default/, 2) end true end |