Class: LinuxAdmin::NetworkInterfaceRH
- Inherits:
-
NetworkInterface
- Object
- NetworkInterface
- LinuxAdmin::NetworkInterfaceRH
- Defined in:
- lib/linux_admin/network_interface/network_interface_rh.rb
Constant Summary collapse
- IFACE_DIR =
"/etc/sysconfig/network-scripts"
Constants included from Common
Instance Attribute Summary collapse
-
#interface_config ⇒ Hash<String, String>
readonly
Key value mappings in the interface file.
Attributes inherited from NetworkInterface
Instance Method Summary collapse
-
#address=(address) ⇒ Object
Set the IPv4 address for this interface.
-
#apply_static(ip, mask, gw, dns, search = nil) ⇒ Boolean
Applies the given static network configuration to the interface.
-
#dns=(*servers) ⇒ Object
Sets one or both DNS servers for this network interface.
-
#enable_dhcp ⇒ Object
Set up the interface to use DHCP Removes any previously set static networking information.
-
#gateway=(address) ⇒ Object
Set the IPv4 gateway address for this interface.
-
#initialize(interface) ⇒ NetworkInterfaceRH
constructor
A new instance of NetworkInterfaceRH.
-
#netmask=(mask) ⇒ Object
Set the IPv4 sub-net mask for this interface.
-
#parse_conf ⇒ Object
Parses the interface configuration file into the @interface_config hash.
-
#save ⇒ Boolean
Writes the contents of @interface_config to @interface_file as ‘key`=`value` pairs and resets the interface.
-
#search_order=(*domains) ⇒ Object
Sets the search domain list for this network interface.
Methods inherited from NetworkInterface
#address, #address6, dist_class, #gateway, #mac_address, #netmask, #netmask6, new, #reload, #start, #stop
Methods included from Common
Constructor Details
#initialize(interface) ⇒ NetworkInterfaceRH
Returns a new instance of NetworkInterfaceRH.
12 13 14 15 16 |
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 12 def initialize(interface) super @interface_file = Pathname.new(IFACE_DIR).join("ifcfg-#{@interface}") parse_conf end |
Instance Attribute Details
#interface_config ⇒ Hash<String, String> (readonly)
Returns Key value mappings in the interface file.
9 10 11 |
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 9 def interface_config @interface_config end |
Instance Method Details
#address=(address) ⇒ Object
Set the IPv4 address for this interface
35 36 37 38 39 |
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 35 def address=(address) validate_ip(address) @interface_config["BOOTPROTO"] = "static" @interface_config["IPADDR"] = address end |
#apply_static(ip, mask, gw, dns, search = nil) ⇒ Boolean
Applies the given static network configuration to the interface
97 98 99 100 101 102 103 104 |
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 97 def apply_static(ip, mask, gw, dns, search = nil) self.address = ip self.netmask = mask self.gateway = gw self.dns = dns self.search_order = search if search save end |
#dns=(*servers) ⇒ Object
Sets one or both DNS servers for this network interface
62 63 64 65 66 |
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 62 def dns=(*servers) server1, server2 = servers.flatten @interface_config["DNS1"] = server1 @interface_config["DNS2"] = server2 if server2 end |
#enable_dhcp ⇒ Object
Set up the interface to use DHCP Removes any previously set static networking information
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 77 def enable_dhcp @interface_config["BOOTPROTO"] = "dhcp" @interface_config.delete("IPADDR") @interface_config.delete("NETMASK") @interface_config.delete("GATEWAY") @interface_config.delete("PREFIX") @interface_config.delete("DNS1") @interface_config.delete("DNS2") @interface_config.delete("DOMAIN") end |
#gateway=(address) ⇒ Object
Set the IPv4 gateway address for this interface
45 46 47 48 |
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 45 def gateway=(address) validate_ip(address) @interface_config["GATEWAY"] = address end |
#netmask=(mask) ⇒ Object
Set the IPv4 sub-net mask for this interface
54 55 56 57 |
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 54 def netmask=(mask) validate_ip(mask) @interface_config["NETMASK"] = mask end |
#parse_conf ⇒ Object
Parses the interface configuration file into the @interface_config hash
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 19 def parse_conf @interface_config = {} File.foreach(@interface_file) do |line| next if line =~ /^\s*#/ key, value = line.split('=').collect(&:strip) @interface_config[key] = value end @interface_config["NM_CONTROLLED"] = "no" end |
#save ⇒ Boolean
Writes the contents of @interface_config to @interface_file as ‘key`=`value` pairs and resets the interface
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 111 def save old_contents = File.read(@interface_file) return false unless stop File.write(@interface_file, @interface_config.delete_blanks.collect { |k, v| "#{k}=#{v}" }.join("\n")) unless start File.write(@interface_file, old_contents) start return false end true end |
#search_order=(*domains) ⇒ Object
Sets the search domain list for this network interface
71 72 73 |
# File 'lib/linux_admin/network_interface/network_interface_rh.rb', line 71 def search_order=(*domains) @interface_config["DOMAIN"] = "\"#{domains.flatten.join(' ')}\"" end |