There is something that I am missing in the following code.
hostnames = []
ip_addrs = []
hw_addrs = []
File.open("/etc/dhcp/dhcpd.conf", "r").each_line do |line|
unless line.match('#') # Make sure the line is not commented
if line.match("host-name")
hostname = line.scan(/"([^"]*)"/) # extract the Hostname
elsif line.match("fixed-address")
ip_addr = line.scan(/(\S*);/) # Extract IP addr
elsif line.match("ethernet")
hw_addr = line.scan(/(\S*);/) # Extract the HW address
end
end
hostnames + hostname.to_a if hostname # Protect against `nil' values
ip_addrs + ip_addr.to_a if ip_addr # Same
hw_addrs + hw_addr.to_a if hw_addr # Same
end
puts hostnames.inspect # Should be a list of hostnames...
This should populate the arrays with the values found in the dhcpd.conf file.
If I print the values inside the File.open.each_line block then I get the complete list to STDOUT. When I try to get the values outside of the block I get empty arrays.
I think that the block generates a copy of my variables and works on those, but they don't get passed back out of the block. I'm not sure how the internals work, just a guess.
enddon't make sense. Were those supposed to be assignments?