Using Ping to Notify when a Server Reboots v2.

Working on network workstations, I found I needed a method to tell when they came online. I wanted a script to email me so I could receive it on my PDA when the connection worked. This way I could be working in the wiring cabinet and hear it successfully connect.

The following script can easily be modified to notify when a server is up or down. I've also added an option to loop the script continuously until specific triggers are met. These triggers can be set at the command line, or hard set into the code for quick deployment. You'll just need to put your email address into the script before you run it.

Requirements: Latest version of BLAT (note the path in line 75).


@echo off
REM ======================================================================
REM
REM Batch File -- Created with SAPIEN Technologies PrimalScript 3.1
REM
REM NAME: NOTIFY.BAT
REM
REM AUTHOR: Eric Woodford
REM DATE : 2/13/2008
REM
REM COMMENT: This script can be configured to notify when a specific server is up or down
REM requires BLAT to send SMTP email messages.
REM ======================================================================
cls

set dest=%1
if '%dest%'=='' goto :error

REM address to notify when triggered.
Set emailaddr=eric@example.com
set mailsrvr=mail.example.com

if not '%2'=='' goto :params
rem set this to 1 to trigger when true
set exitwhenup=0
set notifyup=0
set exitwhendown=1
set notifydown=1
goto :repeat

:params
set exitwhenup=%2
set notifyup=%3
set exitwhendown=%4
set notifydown=%5

:repeat

if exist c:\notifydown.txt del c:\notifydown.txt
ping %dest% -n 1 | find /c "Reply from " > c:\NotifyDown.txt

for /F "tokens=1" %%a in (c:\notifydown.txt) DO (
set /A serverstatus=%%a
if '%serverstatus%'=='1' (
set msgsubject=Server Up - %time%
cls && echo %msgsubject%
if '%notifyup%'=='1' call :sendmsg
if '%exitwhenup%'=='1' goto :exit
) ELSE (
set msgsubject=Server Down - %time%
cls && echo %msgsubject%
if '%notifydown%'=='1' call :sendmsg
if '%exitwhendown%'=='1' goto :exit
)
)

goto :repeat

:error
echo %0 - Notify when device is down
echo.
echo parameters:
echo 1. IP address or host name
echo.
echo [Optional]
echo 2. Exit when up (0=no, 1=yes)
echo 3. Notify when up (0=no, 1=yes)
echo 4. Exit when down (0=no, 1=yes)
echo 5. Notify when down (0=no, 1=yes)

goto :exit

:sendmsg
REM This is all one line, upto the GOTO statement.
call c:\bin\blat262\full\blat.exe c:\notifydown.txt -t %emailaddr% -server %mailsrvr% -f %emailaddr% -subject "%dest% %msgsubject%"
goto :EOF

:exit
echo exiting %time%

AttachmentSize
Notify.zip906 bytes

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <blockquote> <center> <hr> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options