I was wondering if there was a simple way to get the public IP address of a computer using Ruby.
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).
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.
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.
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.
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:
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]] }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With