How do I find out what my external IP address is?

Viewed 68054

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, but the TOS don't allow for automated checks.

Any ideas?

13 Answers

http://ipecho.net/plain appears to be a workable alternative, as whatismyip.com now requires membership for their automated link. They very kindly appear to be offering this service for free, so please don't abuse it.

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.

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.

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.

If the router you are behind speak UPnP you could always use a UPnP library for whatever language you are developing in to query the router for its external ip.

Another way is if you have access to a cloud email (yahoo, google, hotmail), send yourself an email. Then view the headers and you should see your IP address in there.

I would look up the exact area but the headers may vary from each implmentation, Look for the received-by and follow that until you get to something that looks like sent-by

EDIT: This answers the how to find IP address, not the via PROGRAMMATIC approach

whatismyip.com or ipchicken.com are very easy to parse.

If you have a webhost or vps you can also determine it, without fear of it randomly going down leaving you stuck.

Related