views:

450

answers:

9

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?

+1  A: 

This belongs on StackOverflow.com

Spencer Ruport
+2  A: 

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.

Ludwig Weinzierl
+2  A: 

Here is an API designed specifically for this kind of thing. It gives you nice parseable XML and provides lots of useful information.

Sam152
XML! That's crazy. Just return a string. If you really need the whole request, HTTP headers are designed to be easily parsed to begin with.
guns
XML response is great, make parsing zero effort, which is better then easily parsed. Here's my one liner: ([xml](new-object net.webclient).DownloadString("http://ip-address.domaintools.com/myip.xml")).dnstools.ip_address
Scott Weinstein
A: 

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.

whatsisname
+13  A: 

Be nice and use

http://www.whatismyip.com/automation/n09230945.asp

for automated requests (they provide that page specifically for automated requests). Plus, you don't have to bother with any parsing.

Kevin L.
For more info about why this is the preferred way:http://www.whatismyip.com/automation
Justin
+3  A: 

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.

guns
I added a script like this to my server years ago and it is very useful. Everything else is bizarre overkill.
Alex JL
A: 

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

Wayne
That's an extremely inefficient idea.
Shadow
Maybe inefficient but if you are on a locked down workstation and cannot get to the external device its another method you may use.
Wayne
+1  A: 

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.

Eld
A: 

If you're on a windows machine, another option may be this windows script from Daily Cup of Tech.

Michelle
how does the program work?
Scott Weinstein