I have a Raspberry Pi behind NAT running Linux and hosting a web server. I access the web page from outside via Yaler utility as I have Dynamic public ip address. Is there a way to show my current public ip address on the web page? If I get my public ip address, I can access many of the NATted devices in that network. Yaler domain does not resolve to my actual public ip address.
1 Answer
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.
-
IPv6 is sadly still rather uncommon, but I wanted to make a note that in its current for this works for IPv4 only. Also +1 since it is 99% guaranteed to be the answer the OP as well as most site visitors will be looking for.– HennesNov 23, 2014 at 9:26
-
Well, you could probably extend the regular expression to catch IPv6 as well. Unfortunately I don't have a public one so can't test it.– MarioNov 23, 2014 at 10:05
-
The Yaler domain does not resolve to my ip address. My session is with Yaler domain and Yaler redirects that session to my ip address via Reverse http. Nov 23, 2014 at 12:09
-
-
If I have to manage my web server remotely via ssh. Also, getting the public ip address would allow me to access many other devices which are behind the NAT. Nov 23, 2014 at 12:54