2016-06-23 02:08:02 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module GuestLinux
|
|
|
|
module Cap
|
|
|
|
class NetworkInterfaces
|
2016-08-12 00:01:36 +00:00
|
|
|
@@logger = Log4r::Logger.new("vagrant::guest::linux::network_interfaces")
|
|
|
|
|
2016-06-23 02:08:02 +00:00
|
|
|
# Get network interfaces as a list. The result will be something like:
|
|
|
|
#
|
|
|
|
# eth0\nenp0s8\nenp0s9
|
|
|
|
#
|
|
|
|
# @return [Array<String>]
|
2016-06-26 15:41:16 +00:00
|
|
|
def self.network_interfaces(machine, path = "/sbin/ip")
|
2016-06-23 02:08:02 +00:00
|
|
|
s = ""
|
2016-06-26 15:41:16 +00:00
|
|
|
machine.communicate.sudo("#{path} -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |type, data|
|
2016-06-23 02:08:02 +00:00
|
|
|
s << data if type == :stdout
|
|
|
|
end
|
2016-08-12 00:01:36 +00:00
|
|
|
ifaces = s.split("\n")
|
|
|
|
@@logger.debug("Unsorted list: #{ifaces.inspect}")
|
|
|
|
# Break out integers from strings and sort the arrays to provide
|
|
|
|
# a natural sort for the interface names
|
|
|
|
ifaces = ifaces.map do |iface|
|
|
|
|
iface.scan(/(.+?)(\d+)/).flatten.map do |iface_part|
|
|
|
|
if iface_part.to_i.to_s == iface_part
|
|
|
|
iface_part.to_i
|
|
|
|
else
|
|
|
|
iface_part
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end.sort.map(&:join)
|
|
|
|
@@logger.debug("Sorted list: #{ifaces.inspect}")
|
|
|
|
ifaces
|
2016-06-23 02:08:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|