My computers are sitting behind a router/firewall. How do I programmatically find out what my external IP address is. I can use http://www.whatsmyip.org/ for ad-hoc queries,
curl ifconfig.me
or
curl ifconfig.me/ip
Incase you don't have curl installed,
wget ifconfig.me/ip 2>/dev/null && cat ip
Hope this helps.
Since this question was asked a while back, there's now a freely available web service designed specifically to allow you to determine your IP address programmatically, called ipify.
$ curl 'https://api.ipify.org?format=json'
Results in
{"ip": "1.2.3.4" /* your public IP */}
ifcfg.me allows Lookup via
nslookup
telnet
ftp
and http
even works with IPv6
Simple but not elegant for this use. I created a VBS file with the following code to drop the result to dropbox and google drive ... have to delete the file for new one to sync though for some reason.
This runs on a PC at my home. My PC is set to resume on power outage and a task is scheduled to run this every day once (note if you have it run often, the site will block your requests).
Now I can get my IP address on the road and watch people steal my stuff :-)
get_html "http://ipecho.net/plain", "C:\Users\joe\Google Drive\IP.html"
get_html "http://ipecho.net/plain", "C:\Users\joe\Dropbox\IP.html"
sub get_html (up_http, down_http)
dim xmlhttp : set xmlhttp = createobject("msxml2.xmlhttp.3.0")
xmlhttp.open "get", up_http, false
xmlhttp.send
dim fso : set fso = createobject ("scripting.filesystemobject")
dim newfile : set newfile = fso.createtextfile(down_http, true)
newfile.write (xmlhttp.responseText)
newfile.close
set newfile = nothing
set xmlhttp = nothing
end sub
If you have access to a webserver with modphp, you can roll your own:
<?php print $_SERVER['REMOTE_ADDR']; ?>
If you don't want that to get abused, you'll have to keep it secret or add request limits.
I've been using one on my server for years.
Explicitly:
Create a file called whatismyip.php in your public_html folder in your website. It can be called anything and be anywhere in your webroot.
Add the line above, and then query your server:
curl http://example.com/whatismyip.php
for example.
Unfortunately there is no easy way to do it.
I would use a site like www.whatsmyip.org and parse the output.
checkip.dyndns.com returns a very simple HTML file which looks like this:
<html> <head> <title>Current IP Check</title> </head> <body> Current IP Address: 84.151.156.163 </body> </html>
This should be very easy to parse. Moreover the site is exists for about ten years. There is hope that it will be around for a while.