views:

69

answers:

3

Hello,

I have Rails 2.3.8, Ruby 1.8.7, Mongrel Web Server and MySQL database.

I need to find the IP address but I am facing problems. I am in the development mode.

I googled and found code, that its simple to get the IP address using request.remote_ip but, I am getting the output only as 127.0.0.1

In the controller, I have

def index
  @title = "Index Page"
  @client_ip = request.remote_ip
  @remote_ip = request.env["HTTP_X_FORWARDED_FOR"]
  @my_ip = request.env["HTTP_X_FORWARDED_FOR"] || request.remote_addr
  @ip_addr = request.env['REMOTE_ADDR']
end

In the view, I have this code

Your IP address is <%= @client_ip %>  <br/>
AG IP address is <%= @remote_ip %> <br/>
My Personal IP address is <%= @my_ip %> <br/>
IP address is <%= @ip_addr %> 

The output, in the web browser I am getting is

Your IP address is 127.0.0.1
AG IP address is
My Personal IP address is 127.0.0.1
IP address is 127.0.0.1 

I know I am getting 127.0.0.1 because I am developing on the local machine.. but is there a way to get the real ip-address even if I am on the local machine?

Looking forward for your help and support.

Thanks

+3  A: 

As far as I can see there is no standard method for getting the remote address of your local machine. If you need the remote address for (testing) your geocoding, I suggest adding 127.0.0.1 to your database table that matches IP addresses with locations.

As a last resort you could also hard code your remote address. E.g. like this:

class ApplicationController < ActionController::Base
  def remote_ip
    if request.remote_ip == '127.0.0.1'
      # Hard coded remote address
      '123.45.67.89'
    else
      request.remote_ip
    end
  end
end

class MyController < ApplicationController
  def index
    @client_ip = remote_ip()
  end
end
captaintokyo
+4  A: 

I don't think you will get your 'real-ip' on localhost and the reason for this actually lies in TCP/IP.

The remote ip address depends upon the network the request is being sent to. Since it is a part of TCP/IP to send your IP address when communicating, it always translates to relative IP address that the destination computer may understand. When the request is within your subnet, it is your local IP. The moment it goes out to the network through your service provider, it assumes a global IP, which can be tracked back to the service provider (Note: this is arguable and depends on your network setup).

So if you are testing locally then it would be 127.0.0.1 as you have experienced. If you are sending it over your local network, it'll be your IP within that network. For example, my machine's IP in my office network is 192.168.1.7, and if I access my app at 3000 port through another computer in the same network, say from 192.168.1.13, then I get the request.remote_ip as 192.168.1.13

What this is means is, while you are on localhost, 127.0.0.1 is your real IP. And likewise in the example I mentioned, 192.168.1.13 is the real IP for the machine that sent the request.

Swanand
A: 

Depends on how you access the url.

If you access the url using http://127.0.0.1/.... or http://localhost/.... , you'll get 127.0.0.1 as the remote ip. If you are on a LAN, use http://{lan-ip}/....

for e.g. http://172.20.25.210/.......

letronje