Friday, February 15, 2008

Circuit monitoring batch script...

I usually try not to bring work into my blog...but I thought this was kind of cool after I got it working. It is a batch script that will monitor a particular IP address, you can add if you would like, on a network. In my case it was to monitor a gateway router to ensure a circuit was not down. I used a combination of DOS's choice.com and a command line email program called mailsend.exe. Choice.com is simply used as a counter...up to 99. So the script will repeat itself every 99 seconds. Mailsend.exe reads a text file and send a notice to an email address that the circuit did not reply to a ping.

This has only been tested briefly in Windows XP...I did not try it in any other OS and I have not done too much digging into the inner workings...but I imagine it would work for small shops that do not want to purchase software to do it.

If you use it, please leave a comment and let me know what you are using it for, and upload any changes you have made.

Enjoy!

@ECHO OFF
rem This was created to monitor a T1 circuit and router.
rem It will send a ping to the router, attempting 4 times, to connect.
rem If it connects it will wait 1.65 minutes until the next attempt.
rem If it does not connect it sends an email to a user.
rem Needed files: choice.com, mailsend.exe, message.txt, up.txt, down.txt, cirmon.bat

SET PATH = C:\cirmon

:INTRO
rem Replace XXX.XXX.XXX.XXX with the IP you wish to monitor.

for /f "tokens=3" %%a in ('ping XXX.XXX.XXX.XXX ^ find "Reply from"') do set Found=%%a

if %Found% == XXX.XXX.XXX.XXX: GOTO WHOOHOO

if NOT %Found% == XXX.XXX.XXX.XXX: GOTO UHOH

:WHOOHOO

c:\cirmon\choice.com /n /t:c,99 /c:cc

echo circuit is up.
echo circuit is up. >> c:\cirmon\up.txt
echo %time% %date%
echo %time% %date% >> c:\cirmon\up.txt

goto INTRO

:UHOH

echo circuit is down.
echo circuit is down. >> c:\cirmon\down.txt
echo %time% %date%
echo %time% %date% >> c:\cirmon\down.txt

c:\cirmon\mailsend.exe -v < message.txt

c:\cirmon\choice.com /n /t:c,99 /c:cc

goto INTRO

I hope you will find this useful. I don't know what kind of format blogger is going to put on the text...but hopefully it shows up ok. If you need the additional files choice.com and mailsend.exe you can either google them...or I can point you in the right direction.

Have a good one!

-Derek

No comments: