Ethernet public IP

I would like my Adruino to know my network's public IP, and I would like it to find out dynamically. So far the only way I can think of is to connect to www.whatismyip.com, make a GET request, and sift through the response for the ip.

Does anyone know of a better way to do this?
If not, does anyone know how to sift through the html response and create an IPAddress from String?

This is a lot of text to sift through (I highlighted where my public IP appears):

<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <meta charset="UTF-8">
        <title>12f10 | Error</title>
        <link rel="stylesheet" media="screen" href="/cdn-cgi/styles/error.css" />
    </head>
    <body>
        <ul id="Alerts">
            <li class="alert">
                <img src="/cdn-cgi/images/challenge/alert.gif" alert="Error!" />,
                <h1 class="cferror_title">Direct IP Access Not Allowed</h1>  <!-- Error #1003 -->
<p class="cferror_msg">You've requested an IP address that is part of the <a href="https://www.cloudflare.com/" target="_blank">CloudFlare</a> network. If you are interested in learning more about CloudFlare, please <a href="https://www.cloudflare.com" target="_blank">visit our website</a>. (Ref. 1003)</p>
                <ul class="cferror_details">
                    <li><p>Timestamp: XXX, XX-XXX-XX XX:XX:XX XXX</p></li>



                    <li><p>Your IP address: XX.XX.XX.XX</p></li>                //This is where the ip is



                    <li><p class="XXX_no_wrap_overflow_hidden">Requested URL: 12f10/0 </p></li>
                    <li><p>Error reference number: 1003</p></li>
                    <li><p>Server ID: FL_12F10</p></li>
                    <li><p>Process ID: PID_1369256816.881-1-4353832</p></li>
                    <li><p>User-Agent: nil</p></li>
                </ul>
                <a class="corner" href="http://www.cloudflare.com/" target="_blank"></a>
                <div class="clear"></div>
            </li>
        </ul>
        <ul id="Footer">


            <li><span>Your IP: XX.XX.XX.XX</span></li>    //It appears again here


            <li class="last"><a href="http://www.cloudflare.com">&copy; 2012 CloudFlare, Inc.</a></li>
        </ul>
    </body>
</html>

Maybe the below would be simpler.

http://checkip.dyndns.com/

Does your public IP address actually change ? It is quite common for the IP address to remain the same even when you are not paying for a fixed one.

zoomkat:
Maybe the below would be simpler.

http://checkip.dyndns.com/

Yes it would! Thanks! I don't know much about HTML... "GET /" doesn't seem to work. What should my request be?

UKHeliBob:
Does your public IP address actually change ? It is quite common for the IP address to remain the same even when you are not paying for a fixed one.

Not a lot, but I would like to be able to use my Arduino in long term applications and/or multiple networks without requiring re-programming each time there is a change.

Not a lot, but I would like to be able to use my Arduino in long term applications and/or multiple networks without requiring re-programming each time there is a change.

Why is it important to know the public facing IP address of the router that the Arduino is connected to? As a client, I can't imagine that it should make a difference to what you are requesting from a server. As a server, I can't imagine why the IP address of some node in the packet link is relevant.

PaulS:
Why is it important to know the public facing IP address of the router that the Arduino is connected to? As a client, I can't imagine that it should make a difference to what you are requesting from a server. As a server, I can't imagine why the IP address of some node in the packet link is relevant.

Right now there is no point.
I just want the "how to" knowledge in my pocket, it's a challenge, it's fun.

Some client test code.

//zoomkat 3-1-13
//simple client checkip test
//for use with IDE 1.0.1 or later
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "checkip.dyndns.com"; // test web page server
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client ip test 3/1/13"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET / HTTP/1.0"); //download text
    client.println("Host: checkip.dyndns.com");
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

Thanks, zoomkat, that's perfect!

zoomkat:

  while (client.connected() || client.available()) { //connected or data available

char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor
  }

Won't that keep reading and printing bytes as long as the client remains connected, regardless of whether there is any input available?

Click this link. It will show your current public IP. You can reboot your router, DSL or cable modem and try again. Typically, your ISP will provide a WAN IP address with DHCP. And, typically it changes on a reboot. Several reboots and you can see if your IP stays the same. Just click on the link:

http://whatismyipaddress.com/

PeterH:

zoomkat:

  while (client.connected() || client.available()) { //connected or data available

char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor
  }

Won't that keep reading and printing bytes as long as the client remains connected, regardless of whether there is any input available?

It will continue to read bytes that may still remain in the buffer after the server connection is closed.