Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get public (remote) IP address

Tags:

I was wondering if there was a simple way to get the public IP address of a computer using Ruby.

like image 208
Jean-Luc Avatar asked Nov 07 '12 12:11

Jean-Luc


People also ask

How do I find the public IP address of a remote computer?

PC internal IP address: Look in Settings > Network & Internet > Status > View your network properties. Find the network configuration with an "Operational" status and then get the IPv4 address. Your public IP address (the router's IP).

How do you get someone's public IP address?

Starting with the simplest way to find someone's IP address is to use one of the many IP lookup tools available online. Resources such as WhatIsMyIPAddress.com or WhatIsMyIP.com offer tools to enter an IP address and search for its free public registry results.

Can you get public IP from cmd?

Open the command prompt: if you have a Start menu in your Windows system, open it and type cmd into the search bar. If you don't have a search bar, click Run instead. Type ipconfig into the command prompt (or the Run box). Find your IP address within the text that pops up.

Can I access an IP address remotely?

In order to access your computer or device remotely, you will need to point your device(s) to a static IP address. Static IP addresses are often very expensive, and many Internet Service Providers (ISPs) don't even offer them to residential customers. The easy and (free) answer is to setup dynamic DNS.


1 Answers

Akamai provides a "What is my IP" page that you can fetch:

require 'open-uri' remote_ip = open('http://whatismyip.akamai.com').read 

There are a few alternatives that do the same thing, though:

  • http://whatismyip.akamai.com
  • http://ipecho.net/plain
  • http://icanhazip.com
  • http://ident.me
  • http://bot.whatismyipaddress.com

You can also use the ipv4 and ipv6 subdomains with icanhazip.com.

If you don't want to depend on a third party, you can roll your own in a one-line rack app and deploy this for free on Heroku or whatever. It takes into account that X-Forwarded-For may contain a comma separated list of proxy IP addresses and only returns the client IP.

# config.ru  run lambda { |env|   remote_ip = env['HTTP_X_FORWARDED_FOR'] || env['REMOTE_ADDR']   remote_ip = remote_ip.scan(/[\d.]+/).first   [200, {'Content-Type'=>'text/plain'}, [remote_ip]]  } 
like image 57
Patrick Oscity Avatar answered Oct 31 '22 12:10

Patrick Oscity