jueves, diciembre 22, 2011

Get notified when an IP changes

FUSILAO de IntelliAdmin.com de Steve Wiseman

I got a question from John this week:

“A few of my clients are using a broadband connection with DHCP addresses for the public interface. Do you know of a script or program that can be used to monitor the IP address and send a email if the IP address changes?”

Good question. It is possible to get IP address lists from VB Script, but we happen to have a utility we have used internally for quite some time. It is called “MyIP.exe”

MyIP.exe has two options. You can get a current list of IP addresses like this:

MyIP.exe /list

It will return a list of your current IP addresses to the command line:

C:\temp>myip /list
10.10.29.231
192.168.68.1
192.168.198.1

The second option is /wait. It will pause until it sees an IP change. When the change happens it will pause for 15 seconds to allow any DHCP addressing to settle down, and then finally return the result to the command line.

How do we set this up to email when we detect an IP change?

We can use blat. Blat is a tool for sending email from the command line.

Here is how I would structure a bat file to use both programs:

:Loop
@ECHO OFF
ECHO IP Address Change > status.log
ECHO ========================= >> status.log
ECHO The computer %COMPUTERNAME% has had an IP address change >> status.log
ECHO This is the new IP list: >> status.log
MyIP.exe /wait >> status.log
blat.exe -f support@intelliadmin.com -to support@intelliadmin.com -server mysmtp.server.com -subject "IP Change" -bodyF status.log
ECHO "Waiting for IP Change..."
goto loop

What is going on in the code above?

First we build up a file called status.log. It is filled with the body of our email. It contains everything except our list of changed IP addresses. Then MyIP.exe will wait until a change happens - when it does, those IPs are written to the end of status.log

Finally, blat sends out the text file with the subject "IP Change". When that is done, it loops back around to wait for a change all over again.

One important thing to note about SMTP - Many internet providers are blocking send from port 25 by default, except to their own email servers. So make sure you test the script out before depending on it.

MyIP.exe can be found in our downloads section:

http://www.intelliadmin.com/index.php/downloads/

Blat can be found here:

http://www.blat.net/

One final note. Most likely you want this IP address information because you need to remote into their computer from time to time. Our Enterprise Remote control has this functionality built in. It will automatically track all IP address changes on remote machines, and you only need a username to find their computer! - Might save you a lot of time and trouble:)

No hay comentarios: