What is my external IP?

Sometimes, when I'm connected to the local network I have to check what is my external IP address. If I'm on an unknown network and have no access to the router or the server configuration I need external IP to check who is the ISP or what services are avaliable behind the NAT.
There are two ways to get the external IP. First, is to check the IP configuration on the NAT server or router, but this works only if:
  • the router is directly connected to the internet, not to the another router on the other lan network,
  • the static IP address is configured,
  • you have an access to the configuration

Second, and most easiest way is to open your browser, go to your favorite search engine, search for 'what is my ip' and click the first result from the top. If you are lucky, that address is your external IP. If you for some reasons have no luck, check the links at the end of this post.
The other thing could be the command line instruction that will return the IP address, so we can use it for other purposes, as a part of some other scripts. One of possible way is to write a script that fetch the website and get the IP address from its content. Most of the WHAT-IS-MY-IP pages are not script friendly since they are regular web pages full of html tags and it is hard to dermine where the IP address is placed and even if you find it you can never be sure that it is correct (you may find not just one). Anyway, if you want to search through the content of the page,
I recommend you to use Regular Expression:

(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})

Specially for this post, I searched for those websites that shows only IP addresses, no html tags and other content.
Here is the list:
http://www.whatismyip.org/
http://www.whatismyip.com/automation/n09230945.asp
http://icanhazip.com/
http://whatismyip.akamai.com/
http://b10m.swal.org/ip
http://b10m.swal.org/cgi-bin/whatsmyip.cgi?just-ip

And the script that has been ported to a different platforms:
Powershell:

(New-Object System.Net.WebClient).DownloadString('http://whatismyip.org')

Python 3:
import urllib.request
page = urllib.request.urlopen('http://whatismyip.org')
ip = page.read().decode("utf8");
page.close()
print (ip)


Python 2:
import urllib
ip = urllib.urlopen('http://whatismyip.org').read()
print ip


Linux (bash) using wget:
#!/bin/sh
wget -qO - http://www.whatismyip.com/automation/n09230945.asp
echo

1 comments:

mathanthas m said...

I use to find my external IP simply by just visiting www.ip-details.com This site show my IP with basic geographical location, ISP details etc.,

Post a Comment