Logo
English Russian German French Spanish Italian
contact usprivacy
   Support Forums
chart
• Backtrace
• VB-to-HTML
OISV - Organization of Independent Software Vendors - Contributing Member
How to get your external IP address

Many Internet users, having access through router and running DHCP, experience the same problem - what is the real IP address assigned to them by ISP?

For example, you have a server and you want it to be accessible from outside. So you forward the router port to your local IP and provide your friends with the router external IP address to access the server. If you are lucky, it will work for a long time, if not - your server may become inaccessible the next minute after you set it. The reason - your ISP changed IP address assigned to you. Your router knows it (and dynamically refreshes all the information required for Internet access), you friends - don't (and keep hitting the server with an old IP address, wondering why it's not responding).

Wouldn't it be nice to be able to check the external (router) IP address and notify interested parties if it has changed? So, what are the options?

Running ipconfig doesn't help - it only returns you an IP address assigned to your computer by router (or static local IP).

Accessing your router configuration is not a trivial task - normally it requires login and either HTTP (if web interface provided) or telnet session to navigate through. As you probably learned the hard way, it's virtually impossible to automate this task. The fact that almost each router has unique configuration interface makes the problem even more complicated.

nslookup, ping, netstat, arp - don't even go there. They all are nice network utilities, but have a completely different purpose(s).

Here is a simple and elegant solution - hitting the webpage on external server. Webserver knows your real IP address (it has to, otherwise how will it return the page you requested?). Now all you need is the page, returning REMOTE_HOST header. Look, there is your real IP address below (unless, of course, you requested it through anonymizer or other redirection service):

38.107.191.104

Writing such script is a trivial task. For example, for webservers, supporting ASP (pretty much all of Microsoft servers), all you need is type

<%=Request.ServerVariables("REMOTE_ADDR")%>

in text editor (Notepad, anyone?) and save the file with .asp extension (GetExternalIP.asp). Copy newly created file to your website and access it via browser (http://your_website/GetExternalIP.asp). You should see your external IP.

Now, every time your ISP assigns you a new IP address, you can go to this page, get your new IP address and mail to your friends. Fascinating, isn't it? Not so ...
Imagine your ISP changing IP every hour. Perfect time to think about some automation.

There is a lot of ways to retrieve HTML page from the webserver. Your browser is one of them. Winsock API is another. WinInet API do the task too. As well as Microsoft Winsock control, which is part of Visual Basic Pro/Enterprise.

We recommend OstroSoft oswinsck.dll library. Why? Because it's developed by OstroSoft and because we like it (OK, maybe we like it because it's developed by OstroSoft). Download it, unzip and register from command-line (needless to say, your computer must run Windows. Sorry UNIX, Mac and mainframe users - didn't expect you to read this article):
regsvr32 oswinsck.dll

Extremely lazy or impatient individuals can skip any further reading and simply download scripts mentioned below.

For the rest of you: create a new text file, name it "check_ip.vbs" (or whatever you want), open it in text editor (Notepad will do) and enter the following code:

Save the file, locate it in Windows Explorer and double-click on it.
If you followed the instructions, you will get the following message (if not, read this article again):

Your external IP address is: 38.107.191.104

If you are accessing Web via HTTP proxy you will need a slightly modified script:

It's time to get on with automation: we need to modify script so it is executed periodically, compares current IP with previously saved, and in case of the change, notifies the chosen ones. For email notification part we are going to use OstroSoft (surprise!) SMTP Component - don't forget to download and install it on your system.

Our last script is going to be truly gigantic, but it will cover everything: IP address retrieval (with possible HTTP proxy support), comparing it to previously saved value, and email notification (with optional mailserver authentication).

The last task of the day will be adding this script to Task Scheduler and scheduling it to run few/many times a day (depending on your good nature).

That's all, folks. Please let us know what you think about this little article.
With all the questions, suggestions and such contact info@ostrosoft.com


Copyright © 1996-2010 OstroSoft. All rights reserved. info@ostrosoft.com