From 5c926baf0671a1598aff27dfc598a9a44a9b7d7b Mon Sep 17 00:00:00 2001 From: Reto Kaiser Date: Mon, 31 Mar 2014 10:58:36 +0200 Subject: [PATCH 1/2] Use "ip addr show" to detect ip address --- lib/landrush/cap/linux/read_host_visible_ip_address.rb | 5 ++++- test/test_helper.rb | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/landrush/cap/linux/read_host_visible_ip_address.rb b/lib/landrush/cap/linux/read_host_visible_ip_address.rb index acd9a96..ff3851b 100644 --- a/lib/landrush/cap/linux/read_host_visible_ip_address.rb +++ b/lib/landrush/cap/linux/read_host_visible_ip_address.rb @@ -22,7 +22,6 @@ module ReadHostVisibleIpAddress # TODO: Find a better heuristic for this implementation. # def self.read_host_visible_ip_address(machine) - command = "ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1 }'" result = "" machine.communicate.execute(command) do |type, data| result << data if type == :stdout @@ -30,6 +29,10 @@ def self.read_host_visible_ip_address(machine) result.chomp.split("\n").last end + + def self.command + %Q(ip addr show | awk '$1=="inet" {print $2}' | cut -f1 -d'/' | tail -n1) + end end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index b4f5c6e..4fcb0c4 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -4,6 +4,7 @@ require 'minitest/spec' require 'landrush' +require 'landrush/cap/linux/read_host_visible_ip_address' require 'minitest/autorun' @@ -84,7 +85,7 @@ def fake_machine(options={}) machine.instance_variable_set("@communicator", RecordingCommunicator.new) machine.communicate.stub_command( - "ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1 }'", + Landrush::Cap::Linux::ReadHostVisibleIpAddress.command, "#{options.fetch(:ip, '1.2.3.4')}\n" ) From aa751694202551157451d1c5f7860ccb97cb2c88 Mon Sep 17 00:00:00 2001 From: Reto Kaiser Date: Wed, 9 Apr 2014 16:14:59 +0200 Subject: [PATCH 2/2] Use "hostname -I" for ip address detection --- lib/landrush/cap/linux/read_host_visible_ip_address.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/landrush/cap/linux/read_host_visible_ip_address.rb b/lib/landrush/cap/linux/read_host_visible_ip_address.rb index ff3851b..99766d5 100644 --- a/lib/landrush/cap/linux/read_host_visible_ip_address.rb +++ b/lib/landrush/cap/linux/read_host_visible_ip_address.rb @@ -31,7 +31,7 @@ def self.read_host_visible_ip_address(machine) end def self.command - %Q(ip addr show | awk '$1=="inet" {print $2}' | cut -f1 -d'/' | tail -n1) + %Q(hostname -I | awk -F' ' '{print $NF}') end end end