Skip to content

Commit

Permalink
Use "ip addr show" to detect ip address
Browse files Browse the repository at this point in the history
  • Loading branch information
njam committed Mar 31, 2014
1 parent 775d672 commit 5c926ba
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/landrush/cap/linux/read_host_visible_ip_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ 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
end

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
Expand Down
3 changes: 2 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'minitest/spec'

require 'landrush'
require 'landrush/cap/linux/read_host_visible_ip_address'

require 'minitest/autorun'

Expand Down Expand Up @@ -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"
)

Expand Down

0 comments on commit 5c926ba

Please sign in to comment.