I am looking for a way to determine the interface associated to a given IP. Right now I have the opposite where I can return the IP for an interface.
def ipv4_for_interface(i)
return unless node['network']['interfaces'].has_key?(i)
addr, data = node['network']['interfaces'][i]['addresses'].find { |x| x[1]['family'] == 'inet' }
addr
end
For my specific purpose I could do some really dumb iterating through an array and do pattern matching, but I'd like to know how to do it generally.
The internet is full of examples of how to do the opposite of what I'm looking for.
My current hack is this and I really don't like it.
def first_matching_ipv4(match_method)
all = all_matching_ipv4(match_method)
return all[0] unless all.empty?
end
def first_private_ipv4
first_matching_ipv4(:private_ipv4?)
end
ruby_block 'get private' do
block do
node.default['return_val']=$(ifconfig | grep -B1 "inet addr:#{first_private_ipv4}" | awk '$1!="inet" && $1!="--" {print $1}')
end
end
ruby_block 'get public' do
block do
node.default['return_val']=$(ifconfig | grep -B1 "inet addr:#{first_public_ipv4}" | awk '$1!="inet" && $1!="--" {print $1}')
end
end