can any one please help how to get client IP and also server IP using Ruby on Rails
+4
A:
From your controller:
request.remote_ip
If you are using apache in front of a mongrel, then remote_ip will return the source address of the request, which in this case will be local host because the Apache web server is making the request, so instead put this in your controller:
@remote_ip = request.env["HTTP_X_FORWARDED_FOR"]
To get the server IP see:
http://stackoverflow.com/questions/42566/getting-the-hostname-or-ip-in-ruby-on-rails
karim79
2009-08-04 11:34:32
This is working but I need both client Ip and also server IP together
Senthil Kumar Bhaskaran
2009-08-04 11:36:11
@Senthil - see my edit
karim79
2009-08-04 11:38:25
That is because your server is proxying the request to the rails server. You must set up your front server (apache, or whatever you may have) to send also the original request's IP. Can you give more details about the configuration you have?
Vlad Zloteanu
2009-08-04 11:40:56
Thanks Good Job buddy it is working
Senthil Kumar Bhaskaran
2009-08-04 11:45:58
+1
A:
Thanks: karim79 and Titanous
Write the code in Controller
For Client IP:
request.remote_ip
@remote_ip = request.env["HTTP_X_FORWARDED_FOR"]
For Server IP:
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
Senthil Kumar Bhaskaran
2009-08-04 11:49:19