Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / XML

MonitorRouterIP

4.50/5 (2 votes)
29 Oct 2010CPOL12 min read 28.3K   589  
To help people with dynamic domains keep their DNS entries pointing at their home computer and to check for lost connectivity

Purpose

  • To help people with dynamic domains keep their DNS entries pointing at their home computer.
  • To check for lost connectivity

Introduction

MonitorRouterIP monitors the dynamic IP address of your router. It is assigned to you by your Internet Service Provider (ISP) when you configure your router to use DHCP. If you have a static IP, this program is useless to you. When your ISP assigns your router a new IP, then MonitorRouterIP alerts you in a variety of ways. You select those ways as part of your configuration.

MonitorRouterIP is designed to be used by home / small office networks that have a router with a simple web based interface. One where you access it from a browser, via a URL something like:

http://192.168.0.1

I (the author) use this program to know when I must update my DNS name servers at GoDaddy so my domains reflect my new IP.

This program does not run continually in the background. It needs to be installed as a scheduled task. It should wake up at intervals (I use an hour) and check for changes. If it changes, the program will send (optional) alerts in the form of:

email messages (via smtp)

email messages (via your default email client)

sms messages

running a program

messages displayed on the monitor

The program has a relatively small footprint and does not use much in the way of resources. Normally, it just reads the History file to get the last-known IP address of the router. It then gets the current address and compares them. If they are the same, then the program exits. This may be repeated for weeks or months until one day your IP changes. At this point, the alerts are sent out and the history file is updated with a new last-known IP address. You will not see any more alerts until the router IP address changes again.

Installation

There is no auto-installation method. It has to be done manually. There is a pre-requisite that you have to have Microsoft .NET 3.5 Framework installed. You will already have this on Windows 7, Vista-SP2 and XP -SP3. You can get it here.

1. Copy Files

Copy the following files to any directory. (We will assume c:\utility for the sake of this documentation.)

  • MonitorRouterIP.exe
  • MonitorRouterIP.rft (this file)

2. Run the Program

Run the program for the first time. It will ask you if you want to create a Configuration file. Just say OK. This will also create a new History file.

3. Edit the Configuration File

You should automatically be put in an editor for the Configuration file. If not, then manually edit the file:

c:\utility\MonitorRouterIP.config.xml

Don't expect to get everything right the first time. The Configuration file defines three types of data. The first is the <ping> information which is used to determine connectivity. This next is the <router> section which defines how to access the router's IP. Finally there are messages. These are in the <alerts> section.

<ping>url="www.rogers.com"</ping> section

This section of the configuration file in concerned with checking your connection to the internet. You should supply the URL of a site you can always access. I suggest your ISP - like my example above. If that site isn't working, chances are your aren't either. Just ensure it does respond to a ping by opening a command window and issuing the command:

ping www.rogers.com

After the first run, MonitorRouterIP remembers the ping IP in the configuration file and uses that rather than the URL. It's quicker and will work even if there is a DHCP failure.

<router> section

This section of the configuration file is concerned with connecting to the router, navigating to a suitable URL and then extracting the WAN IP address from the router. So the first thing to do is go to your router's configuration web pages. In the browser, you navigate to an address such as: http://192.168.1.0, 192.168.0.1 or 192.168.10.1. Here is a list of router manufacturers and default IP addresses, user names and passwords.

Then, you will be prompted for a user and password. These are what go in the <user> and <password> values. The user is likely to be 'admin'. So this gives us:

XML
<user>admin</user
<password>MyPassw0rd</password

Then hunt around until you find a page with the WAN IP on it. This is the <url> in the configuration file. On my dlink router, it is...

XML
<url>http://192.168.0.1/st_device.html</url>

Now find out which occurrence of the IP address you want on that page. On my status page, it first displays my local IP address (192.168.0.1), then it shows the subnet mask (255.255.0.0) and then it shows my WAN IP address. It is the third IP address on the page so I need:

XML
<occurence>3</occurence>

So that's the router section for my D-Link 524. Yours will be different.

XML
<router>
  <user>admin</user>
  <password>NotTelling</password>
  <url>http://192.168.0.1/st_device.html</url>
  <occurence>3</occurence>
</router>

<alerts> Section

This deals with how the MonitorRouterIP informs you when the IP address changes, connectivity is lost or something goes wrong. There are a number of ways you can be informed and they are each in their own section:

  • <sms>

    Send a text message. This is under development. Do not use it.

  • <display>

    Put messages on your monitor

  • <edithistory>

    Bring up the IP history file on the monitor

  • <email>

    Send a message using your default email client

  • <smtp>

    Send emails via Hotmail, Gmail, your ISP's email, your own SMTP server

  • <run>

    Execute a program, batch file, PowerShell script, etc.

In each section, you will see an attribute (that's the official XML name for it) in the form:

XML
<xxxx use="true">

If you set it to use="false", then this will disable that alert only.

If you have <alerts use="false">, then it will disable all alerts no matter what the individual alerts say about "use". The only time I use is during demos / presentations elsewhere where I am connected to another network, or when I connect to a neighbour's wireless router so I can see my site from 'the outside".

<display use="true" duration="300" />

This causes a message to appear on the monitor, one for each error it finds. The duration is how long the message will stay on the screen. 300 means 5 minutes. duration="0" means the messages will stay till you close them (or your PC) down.

The program continues to run while the messages are displayed. If you are running the MonitorRouterIP as a task, you may well decide to kill it if it runs for more than a certain time. See the section below called scheduling.

<email use="true" />paul_fakename_cotter@gmail.com></email>

This sends a message via your default email client such as Outlook, Window Live or Eudora. There is another way of sending messages using SMTP as shown below. So why have both? SMTP delivers messages then and there. If there is no Internet, then no messages are sent. They are not buffered. You email client takes message and saves them. It will deliver them when it can.

You are likely to get a message saying something like "A program is trying to send an email message on your behalf. Do you want to allow this?". You can normally change this in your client. In Windows Mail, this is on the Security tab of the Options window under the Tools menu. It is a checkbox saying "Warn me when other applications try to send mail as me." This will be addressed in the next release.

<edithistory use="true">

If the IP address changes, then the file (say) c:\utility\MonitorRouterIP.history.xml will be brought up on the screen, probably in an editor. This does not timeout. Its use is somewhat limited.

<smtp use="true">

This is how you send emails. It is a bit complicated, but if you already have your email client working, then it should be fairly painless. If you only use webmail, then it may be more like extracting teeth. The subsection under <smtp> defines how to connect to the email network and how to construct the email. Let's look at each one and compare it to your email client. So look at an email client account. In my example below, I use Windows Mail, but all mail clients are pretty similar and need exactly the same information.

You can have multiple <smtp> entries. I have a local SMTP server I also send to. One of the problems is that if your router loses connectivity, then it cannot send the emails. Note that if this program cannot send email alerts, then they just do not happen. They are not stored for later despatch.

XML
<server>smtp.rogers.com<\server>

This will be the same as your outgoing mail (SMTP) server in Windows mail.

(Aside: If you run an SMTP server, it can also be in the form <server>127.0.0.1</server>.

XML
<from>paul_fakename_cotter@gmail.com</from>

This is the 'who' it will come from. Your mail server may not permit you to put something other than your real email address. Pointless to do otherwise, really. In Windows Mail, this is your "E-mail address:" on the General tab.

XML
<to>paul_fakename_cotter@gmail.com</to>

This is the main addressee for the alert email. Make sure it is real:

XML
<cc>paul_still_fake_cotter@hotmail.com</cc>

You can also copy other addresses such as internal emails to administration, etc. You can have as many <cc> entries as you want.

XML
<replyto>paul_fakename_cotter@gmail.com</replyto>

This is the standard Reply Address on the General tab of Windows mail.

XML
<subject>Problem / Change of IP for router (gmail)</subject>

This is standard subject for the email. If you do not specify this, a suitable subject will be generated.

XML
<port>578</port>

The standard SMTP port is 25. If your ISP is also your email provider, it will likely be 25. For example, my ISP is Roger, my email address is pr_fake_address@rogers.com and the port is 25.

However, if I try ro send mail to a different SMTP server on port 25, then Rogers will block it. This is common behaviour from ISP to stop spam originating from your address.

Most non-ISP email providers use one or more different ports. They can also be different if you are using a secure connection. (That means your messages get encrypted.) For example, Gmail uses port 578 with a secure connection.

I will try to get the port numbers for the main email providers, Gmail, Hotmail, Yahoo, etc. You can send them to me if you like.

XML
<timeout>30</timeout>

You will normally see this on the advanced tab of your email client set up. Here is the time in seconds that ChekRouterIP will try to connect to your email provider. 60 should be more than adequate. If you cannot contact your provider in that time, then you probably have an error somewhere else in this <smtp> configuration. (e.g., port, secureconnection, authentication.)

XML
<authentication>true</authentication>

Almost all email providers require you to provide authentication. (i.e., supply a user name and password). The only time this may not be true is in a small office where you run your own email server. In Windows Mail, it is equivalent to the checkbox:

My Server requires authentication

Currently this program only supports the same setting security for outgoing mail as the incoming mail. If that is a problem, then set yourself up a Gmail or Hotmail account which you can use.

XML
<user>paul_fakename_cotter@gmail.com</user>

This is a totally different user to the one in the <router> section. This is the username you need to connect to your email provider. Usually, it is your email address. This is true of Gmail, Hotmail, Rogers, etc. Some email providers lose the '@' bit:

XML
<user>paul_fakename_cotter</user>
XML
<password>Y0ur_passw0rd</password>

Your password for the email provider. Once again, this is a totally different password to the one in the <router> section.

XML
<secureconnection>true</secureconnection>

This is defined by your email client. It causes things to be encrypted. Some sites demand you use this. Some demand you don't. And for some, it is optional - but they use different ports to connect.

<run use="true" />explorer.exe /e,/separate,c:\utility</email>

This will execute the program you specified. Here we are opening Explorer in a new window pointing at out "c:\utility" directory.

You are likely to get a message saying something like "A program is trying to send an email message on your behalf. Do you want to allow this?". You can normally change this in your client. In Windows Mail this is on the Security tab of the Options window under the Tools menu. It is a checkbox saying "Warn me when other applications try to send mail as me." This will be addressed in the next release.

Testing

You will need to test your alerts. When you first run the program, it will create a history file and a configuration file. The configuration file will be wrong and you will then edit it. It may still be wrong but you need to test it.

Edit the MonitorRouterIP.History.xml file. In the section, <current> alter the IP address. When you re-run the program, it will fire the alerts as the Router WAN IP no longer matches what is in the history file. Keep on changing the configuration file till you get it right. You will have to re-edit the History file every time in order to fire the alerts.

After you have got it working, you can then add it as a scheduled task...

Scheduling

You will almost certainly want to schedule this program. (If you decide to use it, that is.)

Here are my recommendations for setting it up as a scheduled task in Vista. It is going to be the very similar for Windows 7 or XP-SP3.

  1. Run "Task Scheduler". Depending on your system it may be:
    • > Start> then type in Task Scheduler and press enter
    • > Start> Administrative Tools > Task Scheduler
  2. Menu > Action > Create Task

In the Create Task window that pops up:

General Tab

Name: MonitorRouterIP

Description: Watches the router for when it changes its WAN IP address. When that happens, it alerts the user.

Run whether the user is logged on or not = ticked:

Triggers Tab

Press "New..."

In the New Trigger window:

Begin the task: On a schedule
Daily
Delay task for up (Random delay): (ticked) = 1hour
Repeat task every: (ticked) = 1 hour for a duration of : = 1 Day
Stop task if it runs longer than: 30 Minutes
Enabled: (Ticked)

Actions tab

Press "New..."

In the New Action window

Action: Start a program Program/script: c:\utility\MonitorRouterIP.exe (i.e. wherever you put it)
Start in: c:\util.

Conditions tab

Nothing to do unless you want to specify that the network is available.

Settings tab

Allow task to be run on demand (ticked).

Press OK.

When it asks for User name and Password, enter your logon id and password. This information is not available to MonitorRouterIP. It is needed by the Task Scheduler to supply the credentials that the program will run under.

Good luck!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)