On Sun, Oct 7, 2012 at 12:20 PM, ajay paswan <lists / ruby-forum.com> wrote:
> Hi how can I know my IP through which two computers communicate in LAN?
> Like in socket, I wanna send my IP to the target machine.
>
> --
> Posted via http://www.ruby-forum.com/.

You can can use the system method, as in...

system "<whatever command you would use to check your ipaddress on your OS>"

But, you can easily do it without forking another shell.

Somebody else wrote this...

require 'socket'
def local_ip
  orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup,
true  # turn off reverse DNS resolution temporarily
  UDPSocket.open do |s|
    s.connect '64.233.187.99', 1
    s.addr.last
  end
ensure
  Socket.do_not_reverse_lookup = orig
end

p local_ip


It checks the ip it would use to route to a random address without out
sending any packets.
Todd