Either way you'll need some simple script for this.
One easy example would be using DNS lookup.
If you can't use the host
command, install it using sudo apt-get install host
.
Then simply run the command host -t a your.public.domain
. This will result in a line like this:
your.public.domain has address 12.34.56.78
You'll just have to parse the result, e.g. in PHP you could use something like this:
<?php $ip = 'unknown'; if (preg_match('/has address (\d+\.\d+\.\d+\.\d+)/', exec('host -t a your.public.domain'), $m)) $ip = $m[1]; echo "The public IP is $ip.";
Keep in mind that this will delay code execution by about a second; so you should cache the result.